How do I implement hex2bin()?

后端 未结 8 1838
野趣味
野趣味 2020-11-29 02:40

I need to communicate between Javascript and PHP (I use jQuery for AJAX), but the output of the PHP script may contain binary data. That\'s why I use bin2hex()

8条回答
  •  清歌不尽
    2020-11-29 03:32

    function hex2bin(hex)
    {
        var bytes = [], str;
    
        for(var i=0; i< hex.length-1; i+=2)
            bytes.push(parseInt(hex.substr(i, 2), 16));
    
        return String.fromCharCode.apply(String, bytes);    
    }
    

    thanks to Andris!


    Other useful information about this topic (dex2bin,bin2dec) can be found here. According to that, here is a bin2hex solution:

    parseInt(1100,2).toString(16); //--> c
    

提交回复
热议问题