This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Conversion Long to byte[]

 
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
Posts: 23878
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
Posts: 23878
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
I'm full of tinier men! And a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic