Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

long in a byte array

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
EDIT:

I totally misread the OP. My response was not relevent.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Force the long into an int, then use ByteBuffer for the conversion of int to long:
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Luke wrote:
That works for values less than 10,000.

Correction: less than Integer.MAX_VALUE.
One byte is 8 bits, you know.
 
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic