简体   繁体   中英

Converting a hexadecimal String to byte array

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.

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 computing 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:

    \nintToConvert: 10101010 10101010 10101010 10101010  
    \nAND "0xff": 11111111 -------- -------- --------
    \nResults in: 10101010 \n
  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

    \n2863311360 => 10101010 10101010 10101010 00000000 \n  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 .


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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM