Wednesday, 5 September 2012

Converting byte array to long

1.long result = ByteBuffer.wrap(index).getLong();
where index is the byte array



2.  if first byte is the least significant byte
long value1 = 0;
for (int i = 0; i < index.length; i++)
{
  value1 = (value1 << 8) + (index[i] & 0xff);
}


3.if  first byte the most significant
long value = 0;
for (int i = 0; i < index.length; i++)
{
  value += ((long) index[i] & 0xffL) << (8 * i);
}

No comments:

Post a Comment