aspose file tools*
The moose likes Java in General and the fly likes long in a byte array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "long in a byte array" Watch "long in a byte array" New topic
Author

long in a byte array

Santoshkumar Jeevan Pawar
Greenhorn

Joined: Aug 19, 2006
Posts: 27
I want to store a long in a byte array, transfer it over a network, then convert it back to a long.And In Java, a long is 8 bytes not 4.
But i want it a 4 bytes,is there any way to convert .
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
EDIT:

I totally misread the OP. My response was not relevent.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13871
    
  10

Well, 4 bytes is obviously not enough to store all possible values that a long can have in Java, so no, there is no way you're ever going to fit an 8-byte large long into 4 bytes.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Force the long into an int, then use ByteBuffer for the conversion of int to long:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
naresh voota
Greenhorn

Joined: Jun 04, 2007
Posts: 9
putting long in int will reduce the precision of the value stored.

long l = 9;
String s= Long.toString(l);
byte[] b = s.getBytes();

hopefully this will help. Thanks.
naresh voota
Greenhorn

Joined: Jun 04, 2007
Posts: 9
putting long in int will reduce the precision of the value stored.

long l = 9;
String s= Long.toString(l);
byte[] b = s.getBytes();

once you pass this byte array over network,

String s = new String(b);
Long l = Long.valueOf(s);

hopefully this will help. Thanks.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3947
    
  17

naresh voota wrote:putting long in int will reduce the precision of the value stored.

long l = 9;
String s= Long.toString(l);
byte[] b = s.getBytes();

once you pass this byte array over network,

String s = new String(b);
Long l = Long.valueOf(s);

hopefully this will help. Thanks.


That works for values less than 10,000. Once you get to 10,000 you need 5 bytes to hold the String, so, you loose even more than you do when you convert to int. Oh, and any value less than 1,000 would take 3 or fewer bytes, so then you would have to test if < 1000, pad byte array, else if > 9999, throw bytes away.


Steve
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Steve Luke wrote:
That works for values less than 10,000.
Correction: less than Integer.MAX_VALUE.
One byte is 8 bits, you know.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
No, if you look at the code Steve is referring to, one byte ends up representing one character in a String representation. So the largest number that can be represented this way is 9999, at four digits. Of course this isn't a very good way to try to fit a long into 4 bytes. Simply casting to int would be easier, and covers a wiser range of possible values.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3947
    
  17

Bauke Scholtz wrote:
Steve Luke wrote:
That works for values less than 10,000.
Correction: less than Integer.MAX_VALUE.
One byte is 8 bits, you know.


That is true for the answers suggesting to use a cast to int. Naresh, whom I was responding to, suggested converting the long to a String, then the String to byte[]. When you do this and have more than 4 characters (digits) you get more than 4 bytes. See:


output:

Length of byte[] for 10000 is: 5
Santoshkumar Jeevan Pawar
Greenhorn

Joined: Aug 19, 2006
Posts: 27
Hello all,

Here we lose the actual value,please check below code when we typecast from long to int.


long l = new Date().getTime(); //1238583275781
int i = (int)myLong; // value becomes 1633321080

System.out.println(new Date(i));
System.out.println(new Date(myLong));

But at the same time i dont want to lose actual value......

Thank You.
Have a Nice Time.

-Santosh
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Santoshkumar Jeevan Pawar wrote:But at the same time i dont want to lose actual value......

Then you have no choice but to increase your array size to 8. A long simply requires 64 bits (= 8 bytes), nothing more and nothing less. By trying to squeeze it into an int you lose information. You can also convert the long to a float (4 bytes), but then you lose precision on the lower end:

Output:
1238583275781
1238583214080

So the only way to keep your actual value is: make sure you send the actual complete long.
 
 
subject: long in a byte array
 
Similar Threads
Design issue
long to byte array
Reading byte array.
Convert image to raw binary data
Conversion Long to byte[]