首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从字符串中获取散列,如hashids

从字符串中获取散列,如hashids
EN

Stack Overflow用户
提问于 2014-11-06 22:21:50
回答 2查看 7.6K关注 0票数 19

使用hashids包,我可以从数字中获得散列(使用编码和解码)。

代码语言:javascript
复制
    var Hashids = require("hashids"),
        hashids = new Hashids("this is my salt", 8);
    
    var id = hashids.encode(1);

有没有类似的包可以从字符串中获取哈希值?(带编码和解码)

EN

回答 2

Stack Overflow用户

发布于 2014-11-26 05:39:23

代码语言:javascript
复制
var Hashids = require("hashids");
var hashids = new Hashids("this is my salt");

var hex = Buffer.from('Hello World', 'utf8').toString('hex');
console.log (hex); // '48656c6c6f20576f726c64'

var encoded = hashids.encodeHex(hex);
console.log (encoded); // 'rZ4pPgYxegCarB3eXbg'

var decodedHex = hashids.decodeHex('rZ4pPgYxegCarB3eXbg');
console.log (decodedHex); // '48656c6c6f20576f726c64'

var string = Buffer.from('48656c6c6f20576f726c64', 'hex').toString('utf8');
console.log (string); // 'Hello World'
票数 34
EN

Stack Overflow用户

发布于 2021-06-04 10:39:58

在不使用Node的Buffer.from的情况下获取十六进制(用于hashids.decodeHex)

代码语言:javascript
复制
const toHex = (str: string): string => str.split("")
        .reduce((hex, c) => hex += c.charCodeAt(0).toString(16).padStart(2, "0"), "")
const toUTF8 = (num: string): string =>
        num.match(/.{1,2}/g)
            .reduce((acc, char) => acc + String.fromCharCode(parseInt(char, 16)),"");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26781764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档