How do I implement hex2bin()?

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

    With reference to node.js ( not in browser ).

    Basically it's all over-engineered and does not work well.

    responses are out of alignment and though text-wise they are the same bit wise everything is all over the place :

    curl http://phpimpl.domain.com/testhex.php | xxd
    
    00000000: de56 a735 4739 c01d f2dc e14b ba30 8af0  .Q.%G9.....;.0..
    
    curl http://nodejs.domain.com/ | xxd
    
    00000000: c39e 56c2 a725 4739 c380 c3ad c3b1 c39c  ..Q..%G9........
    00000010: c3a1 37c2 6b30 c28f c3b0                 ..;..0....
    

    The proper way to implement this in node is :

    function hex2bin(hex){
       return new Buffer(hex,"hex");
    }
    
    
    curl http://nodejs.domain.com/ | xxd
    
    00000000: de56 a735 4739 c01d f2dc e14b ba30 8af0  .Q.%G9.....;.0..
    

    Hope this helps.

提交回复
热议问题