Reputation: 1031
i have my function to convert string to hex:
function encode(str){
str = encodeURIComponent(str).split('%').join('');
return str.toLowerCase();
}
example:
守护村子
alert(encode('守护村子'));
the output would be:
e5ae88e68aa4e69d91e5ad90
It works on Chinese characters. But when i do it with English letters
alert(encode('Hello World'))
;
it outputs:
hello20world
I have tried this for converting string to hex:
function String2Hex(tmp) {
var str = '';
for(var i = 0; i < tmp.length; i++) {
str += tmp[i].charCodeAt(0).toString(16);
}
return str;
}
then tried it on the Chinese characters above, but it outputs the UTF-8 HEX:
5b8862a467515b50
not the ANSI Hex:
e5ae88e68aa4e69d91e5ad90
I also have searched converting UFT8 to ANSI but no luck. Anyone could help me? Thanks!
Upvotes: 18
Views: 69964
Reputation: 773
If you want to properly handle UTF8 strings you can try these:
function utf8ToHex(str) {
return Array.from(str).map(c =>
c.charCodeAt(0) < 128
? c.charCodeAt(0).toString(16)
: encodeURIComponent(c).replace(/\%/g,'').toLowerCase()
).join('');
}
function hexToUtf8(hex) {
return decodeURIComponent('%' + hex.match(/.{1,2}/g).join('%'));
}
Demo: https://jsfiddle.net/lyquix/k2tjbrvq/
Upvotes: 1
Reputation: 1547
On Node.js, you can do:
const myString = "This is my string to be encoded/decoded";
const encoded = Buffer.from(myString).toString('hex'); // encoded == 54686973206973206d7920737472696e6720746f20626520656e636f6465642f6465636f646564
const decoded = Buffer.from(encoded, 'hex').toString(); // decoded == "This is my string to be encoded/decoded"
Upvotes: 25
Reputation: 4331
This should work.
var str="some random string";
var result = "";
for (i=0; i<str.length; i++) {
hex = str.charCodeAt(i).toString(16);
result += ("000"+hex).slice(-4);
}
Upvotes: 2
Reputation: 1031
I solved it by downloading utf8.js
https://github.com/mathiasbynens/utf8.js
then using the String2Hex
function from above:
alert(String2Hex(utf8.encode('守护村子')));
It gives me the output I want:
e5ae88e68aa4e69d91e5ad90
Upvotes: 2
Reputation: 11
Another way to do it
function toHex(txt){
const encoder = new TextEncoder();
return Array
.from(encoder.encode(txt))
.map(b => b.toString(16).padStart(2, '0'))
.join('')
}
Upvotes: 1
Reputation: 116654
As a self-contained solution in functional style, you can encode with:
plain.split("")
.map(c => c.charCodeAt(0).toString(16).padStart(2, "0"))
.join("");
The split
on an empty string produces an array with one character (or rather, one UTF-16 codepoint) in each element. Then we can map each to a HEX string of the character code.
Then to decode:
hex.split(/(\w\w)/g)
.filter(p => !!p)
.map(c => String.fromCharCode(parseInt(c, 16)))
.join("")
This time the regex passed to split
captures groups of two characters, but this form of split
will intersperse them with empty strings (the stuff "between" the captured groups, which is nothing!). So filter
is used to remove the empty strings. Then map
decodes each character.
Upvotes: 28