• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Byte to long

 
munna next
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any body pl explain hot to convert byte[] to long value ?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All depends on what's in that byte array. And what order it is in.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving from SCJP to Java In General (Beginner)...
 
Ryan McGuire
Ranch Hand
Posts: 1073
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the byte array has the decimal representation of of a number...


If the byte array has been read from some file, you could get clever and use a ByteArrayInputStream to read bytes out of the bye array and wrap that in (decorate it with) a DataInputStream to convert the bytes to a long. Tom make the code as dense as possible, you could do it all one line:



You'll have to try/catch the IOException that the readLong() might throw. What you do in that case is up to you.

Ryan
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we want to convert a byte[8] to a long, we would do something akin to the following:



(Note that I haven't tested this, and bit-shifting really isn't for beginners anyway.) So, the question is, how do you want a byte array to translate to a long?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic