繁体   English   中英

如何在javascript或jquery中将字符串从ascii转换为十六进制

[英]how to convert string from ascii to hexadecimal in javascript or jquery

我想将字符串从ascii转换为十六进制

我试过了:

var stringing = "";
jQuery.each("SomeText".split(""), function (i, data) {
    stringing = stringing + data.charCodeAt(0)
});

但是这个输出和我在http://www.asciitohex.com/上得到的输出不一样

我需要获得相同的值,因为只有在sharepoint中的KQL中才有效

怎么样

String.prototype.convertToHex = function (delim) {
    return this.split("").map(function(c) {
        return ("0" + c.charCodeAt(0).toString(16)).slice(-2);
    }).join(delim || "");
};

"SomeText".convertToHex();
// -> "536f6d6554657874"

"SomeText".convertToHex(" ");
// -> "53 6f 6d 65 54 65 78 74"

请注意,这将使 Unicode字符失败 仅用于ASCII / ANSI输入。

您还可以使用Buffer将ascii转换为十六进制

let hex = Buffer('Some Text', 'ascii').toString('hex');
console.log(hex);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM