How do I implement hex2bin()?

后端 未结 8 1850
野趣味
野趣味 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:45

    To answer your question:

    function Hex2Bin(n){if(!checkHex(n))return 0;return parseInt(n,16).toString(2)}
    

    Here are some further functions you may find useful for working with binary data:

    //Useful Functions
    function checkBin(n){return/^[01]{1,64}$/.test(n)}
    function checkDec(n){return/^[0-9]{1,64}$/.test(n)}
    function checkHex(n){return/^[0-9A-Fa-f]{1,64}$/.test(n)}
    function pad(s,z){s=""+s;return s.length

提交回复
热议问题