iZume iZume - 2 months ago 11
Javascript Question

;How encode string like \x31, etc?

i saw a code and that code had all string in a array.. And each array index was like: "\x31\x32\x33", etc..

How i can convert for example "hello" to that encode format?

if is possible, there a online encoder?

Answer

If you console log the string sequence, you get the decoded strings. So it's as simple as

console.log('\x31\x32\x33'); // 123

For encoding said string, you can extend the String prototype:

String.prototype.hexEncode = function(){
var hex, i;

var result = "";
 for (i=0; i<this.length; i++) {
    hex = this.charCodeAt(i).toString(16);
    result += ("\\x"+hex).slice(-4);
 }
 return result
}

Now,

var a = 'hello';
a.hexEncode(); //\x68\x65\x6c\x6c\x6f