java - How to get long from byte array returned by jmrtd's sendGetChallenge (used to get passport random number) -


i'm using jmrtd library on android , process passport information.

the first step send challenge passport responds random 64 bit number byte array (8 bytes).

the function sendgetchallenge returns byte array.

i need print out byte array number make analysis i'm having trouble converting byte array returned long, because i'm not sure if array big endian or little endian.

so far i've used following methods:

public static long bytearraytolong(byte[] bytes){     long value = 0;     value += (long) (bytes[7] & 0x000000ff) << 56;     value += (long) (bytes[6] & 0x000000ff) << 48;     value += (long) (bytes[5] & 0x000000ff) << 40;     value += (long) (bytes[4] & 0x000000ff) << 32;     value += (bytes[3] & 0x000000ff) << 24;     value += (bytes[2] & 0x000000ff) << 16;     value += (bytes[1] & 0x000000ff) << 8;     value += (bytes[0] & 0x000000ff);     return value; } 

or

private long bytearraytolong(byte[] buffer){     long value=0;     long multiplier=1;     (int = 7; >= 0; i--) { //get right         value=value+(buffer[i] & 0xff)*multiplier;          multiplier=multiplier <<8;     }     return value; } 

or

public static long bytestolong(byte[] bytes) {     bytebuffer buffer = bytebuffer.allocate(long.size);     buffer.put(bytes);     buffer.flip();     return buffer.getlong(); } 

or

byte[] randomnumber = sendgetchallenge();     new biginteger(randomnumber).tostring(); 

but i'm getting (sometimes) negative values wrong.

can me on why getting negative values, or if know how long value byte array returned sendgetchallenge function jmrtd library.

thank you.

that negative isn't wrong. what's happening long signed number, if first bit of byte array 1 turn entire number negative. holding right value, display negative. solution either deal or not use long (use biginteger or string).


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -