Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encode String to HEX

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!

like image 457
John Pangilinan Avatar asked Apr 15 '16 02:04

John Pangilinan


People also ask

How do I encode a hex string?

Hex encoding is performed by converting the 8 bit data to 2 hex characters. The hex characters are then stored as the two byte string representation of the characters. Often, some kind of separator is used to make the encoded data easier for human reading.

How do you hex encode a string in Python?

To convert Python String to hex, use the inbuilt hex() method. The hex() is a built-in method that converts the integer to a corresponding hexadecimal string. For example, use the int(x, base) function with 16 to convert a string to an integer.

What are hex strings?

Hexadecimal Number String. The “Hexadecimal” or simply “Hex” numbering system uses the Base of 16 system and are a popular choice for representing long binary values because their format is quite compact and much easier to understand compared to the long binary strings of 1's and 0's.


2 Answers

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.

like image 170
Daniel Earwicker Avatar answered Oct 18 '22 14:10

Daniel Earwicker


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"
like image 44
Cassio Avatar answered Oct 18 '22 15:10

Cassio