Encode String to HEX

43,536

Solution 1

I have solve this by downloading utf8.js

https://github.com/mathiasbynens/utf8.js

then using the String2Hex function above:

alert(String2Hex(utf8.encode('守护村子')));

gives me the output i want:

e5ae88e68aa4e69d91e5ad90

Solution 2

const myString = "This is my string to be encoded/decoded";
const encoded = new Buffer(myString).toString('hex'); // encoded === 54686973206973206d7920737472696e6720746f20626520656e636f6465642f6465636f646564
const decoded = new Buffer(encoded, 'hex').toString(); // decoded === "This is my string to be encoded/decoded"

Solution 3

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.

Solution 4

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);
    }
Share:
43,536
John Pangilinan
Author by

John Pangilinan

I'm a simple man, i see codes. I compile.

Updated on July 05, 2022

Comments

  • John Pangilinan
    John Pangilinan almost 2 years

    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!

  • haakym
    haakym about 7 years
    Anyone looking at this question like me should also consider the following: stackoverflow.com/questions/32965971/…
  • nbi
    nbi about 7 years
    anyone looking for answer without third part can refer http://stackoverflow.com/questions/21647928/javascript-unico‌​de-string-to-hex
  • Lukas Eder
    Lukas Eder about 6 years
    This answer isn't really self contained. It relies entirely on an external resource...
  • Armen Michaeli
    Armen Michaeli over 5 years
    FYI: The Buffer class is not standardized by any standards body I know of, including ECMA. It is offered by Node.js, however, and does what you'd think it does.
  • Daniel Earwicker
    Daniel Earwicker over 3 years
    @rumpel true! I’ve added a padStart.
  • Luc
    Luc about 3 years
    Doesn't work in browsers. Not only isn't it standardized like amn says, but also it just doesn't exist, it's only a Node.js thing unfortunately. This means it doesn't answer the question because, obviously, alert() as OP used in the question doesn't make sense in Node.js, they clearly meant for this to work in a browser. But hopefully it still helps others that want to use this in server-side code...
  • Jankapunkt
    Jankapunkt about 3 years
    This should be the accepted answer, since it works in modern browsers and node and does not require additional libraries.