Hex for a string in javascript

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 does not work in chrome, but it works fine in mozila. who can help.

Thanks in advance

+7
source share
1 answer

From your comments it seems that you are calling

 hex2a('000000000000000000000000000000314d464737'); 

and warn the result.

Your problem is that you are building a line starting with 0x00. This code is usually used as a line terminator for a zero-terminated string.

Remove 00 at startup:

 hex2a('314d464737'); 

You can correct your function as follows to skip this 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; } 

Please note that your string, full 0x00, can still be used in other contexts, but Chrome cannot warn it. You should not use this type of string.

+6
source

All Articles