[英]Converting a hexadecimal String to byte array
My question is: 我的问题是:
how to successfully use the method getResourceByHash(...)
of Evernote API? 如何成功使用Evernote API的方法
getResourceByHash(...)
?
What I have done: 我做了什么:
I have got the hex hash of em-media in the note content: 80ad525cd14de8f925487c02afc9ab21
我在笔记内容中得到了em-media的hex哈希:
80ad525cd14de8f925487c02afc9ab21
Then I use the following function to turn the hexadecimal String
to bytes: 然后我使用以下函数将十六进制
String
为字节:
function hex2bin(hex){
var bytes = [];
for(var i=0; i< hex.length-1; i+=2) {
bytes.push(parseInt(hex.substr(i, 2), 16));
}
return String.fromCharCode.apply(String, bytes);
}
var bin = hex2bin("80ad525cd14de8f925487c02afc9ab21");
At next I apply the variable to the function getResourceByHash(...)
in this way: 接下来,我以这种方式将变量应用于函数
getResourceByHash(...)
:
noteStore.getResourceByHash(GUID, bin, true, true, true,
function(err,result){
console.log(err);
console.log(result);
}
);
But the output turns out: 但输出结果是:
{identifier: 'Resources', key: 'c280c2ad525cc3914dc3a8c3b925487c02c2afc389c2ab21'}
undefined
In all, I am confused. 总之,我很困惑。
Further, you cannot simply push a value of type Integer
to a byte array. 此外,您不能简单地将
Integer
类型的值推送到字节数组。 Integers are represented with 32 bit (4 bytes), so you first have to "split" such number while computing the single bytes one by one: 整数用32位(4字节)表示,因此您首先必须“分割”这样的数字,同时逐个计算单个字节:
intToByteArray = function(intToConvert) {
var byteArray = new Array(4)
for(var i = 0; i < byteArray.length; i++) {
var byte = intToConvert & 0xff;
byteArray[i] = byte;
intToConvert = (intToConvert - byte) / 256 ;
}
return byteArray;
};
Demonstration of back and forth conversion (JS-Fiddle) 来回转换演示(JS-Fiddle)
At first, we declare an array of bytes: 首先,我们声明一个字节数组:
var byteArray = new Array(4)
Array: [00000000, 00000000, 00000000, 00000000]
数组:[00000000,00000000,00000000,00000000]
By using the bit-wise AND
operator &
, we "capture" the first 8 bits while assigning the resulting value to a new variable: 通过使用逐位
AND
运算符&
,我们“捕获”前8位,同时将结果值赋给新变量:
var byte = intToConvert & 0xff;
What's happening with our variables:
我们的变量发生了什么:
\nintToConvert: 10101010 10101010 10101010 10101010intToConvert:10101010 10101010 10101010 10101010
\nAND "0xff": 11111111 -------- -------- --------AND“0xff”:11111111 -------- -------- --------
\nResults in: 10101010结果:10101010\n
Then, we put the resulting single byte to the actual index of the temporary byte array: 然后,我们将得到的单个字节放到临时字节数组的实际索引中:
byteArray[i] = byte;
Array: [10101010, 00000000, 00000000, 00000000]
数组:[10101010,00000000,00000000,00000000]
Now, we only have to subtract the value just added to the array and remove one byte from the integer variable: 现在,我们只需要减去刚刚添加到数组中的值,并从整数变量中删除一个字节:
intToConvert = (intToConvert - byte) / 256 ;
2863311530 - 170 = 2863311360
2863311530 - 170 = 2863311360
2863311360 / 256 = 111848102863311360/256 = 11184810
\n2863311360 => 10101010 10101010 10101010 000000002863311360 => 10101010 10101010 10101010 00000000\n 11184810 => 10101010 10101010 10101010
11184810 => 10101010 10101010 10101010\n
Continuing with this loop will transfer byte by byte from the Integer to the temporary byte array. 继续该循环将逐字节地从整数传输到临时字节数组。 So we will get a byte array of 4 single bytes representing one integer or two
Character
of the hexadecimal String
. 因此,我们将获得一个包含4个单字节的字节数组,表示十六进制
String
一个整数或两个Character
。
How to convert byte
to Integer
is well explained here . 这里有很好的解释如何将
byte
转换为Integer
。
So your updated hex2bin(String)
should look like: 所以你更新的
hex2bin(String)
应如下所示:
function hex2bin(hexString) {
var bytes = new Array(hexString.length / 2);
for(var i = 0; i < hexString.length-1; i+=2) {
bytes.push(
intToByteArray(
parseInt(hexString.substr(i, 2), 16)
)
);
}
StringBuilder sb = new StringBuilder(bytes.length * 8);
for(byte b : bytes) {
sb.append(Integer.toBinaryString((b & 0xFF) + 0x100).substring(1));
}
return sb.toString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.