Hexadecimal to string in javascript

javascript

Solution

From your comments it appears you're calling

hex2a('000000000000000000000000000000314d464737');

and alerting the result.

Your problem is that you're building a string beginning with 0x00. This code is generally used as a string terminator for a null-terminated string.

Remove the `00` at start :

hex2a('314d464737');

You might fix your function like this to skip those null "character" :

function hex2a(hex) {
    var str = '';
    for (var i = 0; i < hex.length; i += 2) {
        var v = parseInt(hex.substr(i, 2), 16);
        if (v) str += String.fromCharCode(v);
    }
    return str;
}  

Note that your string full of 0x00 still might be used in other contexts but Chrome can't alert it. You shouldn't use this kind of strings.

Problem

``` function hex2a(hex) { var str = ''; for (var i = 0; i < hex.length; i += 2) str += String.fromCharCode(parseInt(hex.substr(i, 2), 16)); return str; } ``` This function is not working in chrome, but it is working fine in mozila. can anyone please help. Thanks in advance

Original source

Related problems