This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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: 23588
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: 23588
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: 21459
96
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!