Win a copy of Securing DevOps this week in the Security forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Conversion Long to byte[]  RSS feed

 
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
I am trying to convert a Long variable to a byte array.and i don't know where to really start and suggestion would be appreciated
 
author
Sheriff
Posts: 23392
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Abiodun Adisa:
Hello All,
I am trying to convert a Long variable to a byte array.and i don't know where to really start and suggestion would be appreciated



It depends. If this is a homework problem, then your instructor is probably expecting you to use a combination of bit shifting and the AND operator to extract the eight bytes from the long.

If this is *not* a homework problem, then I recommend that you use the java.nio.ByteBuffer class, like this:



Henry
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If this is *not* a homework problem, then I recommend that you use the java.nio.ByteBuffer class[/QB]



Even if it is a homework question, surely one should give an answer that uses the best available APIs, unless specifically told not to do so?
 
Abiodun Adisa
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well thanks all for your input but the byteBuffer is not working for me lets say i have the code to convert a byte array into Long
which is

private long byte2Long(byte[] byteArray)
{
long l=0;
int shift=0;
for (int j=byteArray.length-1;j>=0;j--){
l+=(int)bytearray[j]&0xFF<<(8*shift);
shift++;
}
} given a byte array with 4 bytes [0,0,71,-18] (it is always 4 byte) the function above would return 18176. Now i am trying to convert this back but using your process requires 5 bytes as opposed to 4




Originally posted by Henry Wong:


It depends. If this is a homework problem, then your instructor is probably expecting you to use a combination of bit shifting and the AND operator to extract the eight bytes from the long.

If this is *not* a homework problem, then I recommend that you use the java.nio.ByteBuffer class, like this:



Henry

 
Henry Wong
author
Sheriff
Posts: 23392
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

given a byte array with 4 bytes [0,0,71,-18] (it is always 4 byte) the function above would return 18176. Now i am trying to convert this back but using your process requires 5 bytes as opposed to 4



4 bytes is a int -- not a long. The java.nio.ByteBuffer class can convert in either direction, for either an int or a long. In fact...



Henry
 
Sheriff
Posts: 21256
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Abiodun Adisa:
private long byte2Long(byte[] byteArray)
{
long l=0;
int shift=0;
for (int j=byteArray.length-1;j>=0;j--) {
l+=(int)bytearray[j]&0xFF<<(8*shift);
shift++;
}
}


You can traverse the array from 0 to byteArray.length as well, by left shifting the long each time:


As for the ByteBuffer solution, that does not work for cases when, for some reason, your array size is not at least 8.
 
Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
Thread Boost - a very different sort of advertising
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!