3

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.

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. 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 Commented Mar 12, 2015 at 4:47

1 Answer 1

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

    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();
}
Sign up to request clarification or add additional context in comments.

2 Comments

well, It doesn't work.The converting result applied to the function will output [Error:Cannot read length of binary data] According to the link the contenthash parameter should be a string. And what's more , even if I change ur ways to output string, It still doesnt work..
So I must had found a class from another package, sorry for that. How do you "change my way to output string"? The expected value is 11000010100000001100001010101101010100100101110011000011100100011001101110000111010100011000011101110010010010101001000011111001011000010101011111100001110001001110000101010101100100001

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.