最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Converting a hexadecimal String to byte array - Stack Overflow

programmeradmin4浏览0评论

My question is:
how to successfully use the method getResourceByHash(...) of Evernote API?

What I have done:
I have got the hex hash of em-media in the note content: 80ad525cd14de8f925487c02afc9ab21

Then I use the following function to turn the hexadecimal String to bytes:

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:

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.

My question is:
how to successfully use the method getResourceByHash(...) of Evernote API?

What I have done:
I have got the hex hash of em-media in the note content: 80ad525cd14de8f925487c02afc9ab21

Then I use the following function to turn the hexadecimal String to bytes:

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:

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.

Share Improve this question edited Mar 12, 2015 at 7:12 Marvin Emil Brach 3,9721 gold badge33 silver badges62 bronze badges asked Mar 12, 2015 at 1:33 RickyRicky 331 silver badge4 bronze badges 2
  • Depending on your provided data, there is an error while converting hex to binary or passing that value to the function: the key c280c2ad525cc3914dc3a8c3b925487c02c2afc389c2ab21could not be binary as it contains letters, instead it is the almost same except the trailing "c2" whereever you're adding it. – Marvin Emil Brach Commented Mar 12, 2015 at 2:00
  • thx, I also suspect the wrong converting of the function hex2bin. and do u know the correct ways to do that? thanks – Ricky Commented Mar 12, 2015 at 4:47
Add a ment  | 

1 Answer 1

Reset to default 5

Further, you cannot simply push a value of type Integer to a byte array. Integers are represented with 32 bit (4 bytes), so you first have to "split" such number while puting the single bytes one by one:

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)


Explanation of code lines

  1. At first, we declare an array of bytes:

    var byteArray = new Array(4)
    

    Array: [00000000, 00000000, 00000000, 00000000]

  2. By using the bit-wise AND operator &, we "capture" the first 8 bits while assigning the resulting value to a new variable:

    var byte = intToConvert & 0xff;
    

    What's happening with our variables:

    intToConvert: 10101010 10101010 10101010 10101010
    AND "0xff": 11111111 -------- -------- --------
    Results in: 10101010
  3. Then, we put the resulting single byte to the actual index of the temporary byte array:

    byteArray[i] = byte;
    

    Array: [10101010, 00000000, 00000000, 00000000]

  4. 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
    2863311360 / 256 = 11184810

    2863311360 => 10101010 10101010 10101010 00000000
      11184810 =>          10101010 10101010 10101010
    

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.


How to convert byte to Integer is well explained here.
So your updated hex2bin(String) should look like:

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();
}
发布评论

评论列表(0)

  1. 暂无评论