File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Byte to long Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Byte to long" Watch "Byte to long" New topic

Byte to long

munna next
Ranch Hand

Joined: Sep 28, 2004
Posts: 46
Can any body pl explain hot to convert byte[] to long value ?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
All depends on what's in that byte array. And what order it is in.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Moving from SCJP to Java In General (Beginner)...
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1030
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.

Joel McNary

Joined: Aug 20, 2001
Posts: 1824

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?

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
I agree. Here's the link:
subject: Byte to long