11
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

Aaradhana
  • 129
  • 1
  • 1
  • 7
  • 2
    Seems to work on Chromium for me : http://jsfiddle.net/fDzqu/ – Denys Séguret Dec 04 '12 at 07:16
  • 2
    Could you clarify "not working"? What happens when you try it, and how does that differ from what you expect? Do you get any error message? Works fine in Chrome when I try it. http://jsfiddle.net/Guffa/uT2q5/ – Guffa Dec 04 '12 at 07:24
  • Please give an example input and output. What is it in chrome vs firefix? – loganfsmyth Dec 04 '12 at 07:25
  • @Aaradhana: Any errors messages? – karthick Dec 04 '12 at 07:25
  • it is displaying Only 'A' in alert, try like this 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; } function output() { var text=hex2a(000000000000000000000000000000314d464737); alert('hi'); alert('value'+text); } ​ it is not working:( – Aaradhana Dec 04 '12 at 07:32
  • @karthick : i inserted alert and checked the value of str variable in chrome it is displaying value as [][][][][][][][][][][][][][][]6ghU8 – Aaradhana Dec 04 '12 at 07:42
  • hex2a(000000000000000000000000000000314d464737); can't compile. – Denys Séguret Dec 04 '12 at 07:42
  • I see you found the function here: (http://stackoverflow.com/questions/3745666/how-to-convert-from-hex-to-ascii-in-javascript) on Stackoverflow. It works for me on Chrome and Mozilla. How do you call the function and what do you expect? – Andries Dec 04 '12 at 07:45
  • 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; } function DrawCaptcha(a) { var text= a; var text1=hex2a(text); alert('text'+text1); document.getElementById("txtCaptcha").value=text1; document.captchaform.textval.value==""; } it is running fine in Mozila, but not in chrome, in chrome it is displaing text1 as [][][][][][][][][][][][][][][]6ghU8 in alert box, In mozila 6ghU8 , do i need to add anything ???? – Aaradhana Dec 04 '12 at 07:53
  • @Aaradhana please update your question instead of add cluttered comment. – Édouard Lopez Mar 26 '17 at 18:31

1 Answers1

14

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.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758