最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

how to convert string from ascii to hexadecimal in javascript or jquery - Stack Overflow

programmeradmin2浏览0评论

I want to convert string from ascii to hexadecimal

I tried:

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

But this output is not the same as what I get at /

I need to get the same values because only that works in KQL in sharepoint

I want to convert string from ascii to hexadecimal

I tried:

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

But this output is not the same as what I get at http://www.asciitohex./

I need to get the same values because only that works in KQL in sharepoint

Share Improve this question edited Nov 25, 2015 at 15:50 CoderPi 13.2k4 gold badges38 silver badges64 bronze badges asked Nov 25, 2015 at 15:22 Vignesh SubramanianVignesh Subramanian 7,30914 gold badges96 silver badges158 bronze badges 1
  • I tried this var something="text"; console.log(something.toString(16)), but it didn't work – Vignesh Subramanian Commented Nov 25, 2015 at 15:26
Add a ment  | 

2 Answers 2

Reset to default 6

How about

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

and

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

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

Note that this will fail with Unicode characters. Use it for ASCII/ANSI input only.

You can also use Buffer to convert ascii to hex

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

评论列表(0)

  1. 暂无评论