如何实现hex2bin()? [英] How do I implement hex2bin()?

查看:303
本文介绍了如何实现hex2bin()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Javascript和PHP之间进行通信(我将jQuery用于AJAX),但是PHP脚本的输出可能包含二进制数据.这就是为什么我在PHP端使用bin2hex()json_encode()的原因.

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() and json_encode() on PHP side.

如何使用JavaScript将十六进制字符串转换为二进制字符串?

How do I convert the hexadecimal string in binary string, with JavaScript?

推荐答案

JavaScript不支持二进制数据.不过,您可以使用常规字符串来模拟它.

JavaScript doesn't have support for binary data. Nevertheless you can emulate this with regular strings.

var hex = "375771", // ASCII HEX: 37="7", 57="W", 71="q"
    bytes = [],
    str;

for(var i=0; i< hex.length-1; i+=2){
    bytes.push(parseInt(hex.substr(i, 2), 16));
}

str = String.fromCharCode.apply(String, bytes);

alert(str); // 7Wq

这篇关于如何实现hex2bin()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆