如何在 Javascript 中编程 hex2bin?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7695450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to program hex2bin in Javascript?
提问by Martin Vseticka
I need to communicate between Javascript and PHP (I use jquery for ajax) but the output of PHP script may contain binary data. That's why I use function bin2hex
in PHP. Then json_encode
is applied on PHP side. What I don't know is how to convert hexadecimal string to binary data on Javascript side.
我需要在 Javascript 和 PHP 之间进行通信(我将 jquery 用于 ajax),但 PHP 脚本的输出可能包含二进制数据。这就是我bin2hex
在 PHP 中使用函数的原因。然后json_encode
应用于 PHP 端。我不知道的是如何在 Javascript 端将十六进制字符串转换为二进制数据。
Thanks!
谢谢!
采纳答案by Andris
JavaScript doesn't have support for binary data. Nevertheless you can emulate this with regular strings.
JavaScript 不支持二进制数据。不过,您可以使用常规字符串来模拟这一点。
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
回答by tobspr
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<z?pad("0"+s,z):s}
function unpad(s){s=""+s;return s.replace(/^0+/,'')}
//Decimal operations
function Dec2Bin(n){if(!checkDec(n)||n<0)return 0;return n.toString(2)}
function Dec2Hex(n){if(!checkDec(n)||n<0)return 0;return n.toString(16)}
//Binary Operations
function Bin2Dec(n){if(!checkBin(n))return 0;return parseInt(n,2).toString(10)}
function Bin2Hex(n){if(!checkBin(n))return 0;return parseInt(n,2).toString(16)}
//Hexadecimal Operations
function Hex2Bin(n){if(!checkHex(n))return 0;return parseInt(n,16).toString(2)}
function Hex2Dec(n){if(!checkHex(n))return 0;return parseInt(n,16).toString(10)}
回答by Andris
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:
可以在此处找到有关此主题的其他有用信息 (dex2bin,bin2dec) 。据此,这是一个bin2hex
解决方案:
parseInt(1100,2).toString(16); //--> c
回答by MichaelRushton
Although not an answer to the actual question, it is perhaps useful in this case to also know how to reverse the process:
虽然不是实际问题的答案,但在这种情况下,了解如何反转过程可能很有用:
function bin2hex (bin)
{
var i = 0, l = bin.length, chr, hex = ''
for (i; i < l; ++i)
{
chr = bin.charCodeAt(i).toString(16)
hex += chr.length < 2 ? '0' + chr : chr
}
return hex
}
As an example, using hex2bin
on b637eb9146e84cb79f6d981ac9463de1
returns ?7?FèL·méF=á
, and then passing this to bin2hex
returns b637eb9146e84cb79f6d981ac9463de1
.
例如,使用hex2bin
on b637eb9146e84cb79f6d981ac9463de1
return ?7?FèL·méF=á
,然后将其传递给bin2hex
Returns b637eb9146e84cb79f6d981ac9463de1
。
It might also be useful to prototype these functions to the String
object:
将这些函数原型化为String
对象也可能很有用:
String.prototype.hex2bin = function ()
{
var i = 0, l = this.length - 1, bytes = []
for (i; i < l; i += 2)
{
bytes.push(parseInt(this.substr(i, 2), 16))
}
return String.fromCharCode.apply(String, bytes)
}
String.prototype.bin2hex = function ()
{
var i = 0, l = this.length, chr, hex = ''
for (i; i < l; ++i)
{
chr = this.charCodeAt(i).toString(16)
hex += chr.length < 2 ? '0' + chr : chr
}
return hex
}
alert('b637eb9146e84cb79f6d981ac9463de1'.hex2bin().bin2hex())
回答by Marco Demaio
All proposed solutions use String.fromCharCode
, why not simply using unescape
?
所有建议的解决方案都使用String.fromCharCode
,为什么不简单地使用unescape
?
String.prototype.hex2bin = function()
{
var i = 0, len = this.length, result = "";
//Converting the hex string into an escaped string, so if the hex string is "a2b320", it will become "%a2%b3%20"
for(; i < len; i+=2)
result += '%' + this.substr(i, 2);
return unescape(result);
}
and then:
进而:
alert( "68656c6c6f".hex2bin() ); //shows "hello"
回答by davidsaliba
With reference to node.js ( not in browser ).
参考 node.js (不在浏览器中)。
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.
希望这可以帮助。
回答by Jan Bauer
If someone needs the other direction (bin to hex), here is it:
如果有人需要另一个方向(二进制到十六进制),这里是:
function bin2hex(bin) {
return new Buffer(bin).toString("hex");
}
回答by eb80
JavaScript does actually contain support for binary data. See https://developer.mozilla.org/en-US/docs/JavaScript/Typed_arrays/Uint8Arrayfor example.
JavaScript 实际上包含对二进制数据的支持。例如,请参阅https://developer.mozilla.org/en-US/docs/JavaScript/Typed_arrays/Uint8Array。
To convert to hex, read a byte and then take the each of the two nibbles (groups of four bits) and convert those into hex.
要转换为十六进制,请读取一个字节,然后取出两个半字节(四位组)中的每一个并将它们转换为十六进制。