This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Java in General and the fly likes long Value to byte Array of size 4 Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "long Value to byte Array of size 4" Watch "long Value to byte Array of size 4" New topic
Author

long Value to byte Array of size 4

Karthik Rajendiran
Ranch Hand

Joined: Aug 13, 2004
Posts: 209
Hello Friends,
I have long value ,which i need to store in byte array of size 4.How to do that
Can any one please give me the code?


SCJP 1.4 SCWCD 1.4 SCDJWS 1.4
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16809
    
  19

A "long" value is 64 bits -- which means that it needs a byte array of size 8. Unless of course, you mean an "int" value.

Anyway, here is the easiest way to do this...




BTW, if this is a homework question, it would be highly unlikely that your teacher will accept this solution. He/She is probably looking for a combination of shift/and/or operators.

Henry
[ October 27, 2006: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Karthik Rajendiran
Ranch Hand

Joined: Aug 13, 2004
Posts: 209
Hello Henry
Thanks for your input. BTW it is not homework question.
We need to store one CRC value long in a byte array of size 4
Is it possible to do some base64 coding or compression technique?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16809
    
  19

Is it possible to do some base64 coding or compression technique?


Base64 will actually expand the size -- every 5 bits will become 8 bits.

As for compression, unlikely. A CRC is a checksum. No common patterns. No large groups of ones or zeros. It is unlikely to get any compression on a CRC, much less get it down to half it's size.


BTW, I always thought CRC was much smaller than 64 bits. When I used them, more than a decade ago, they were only 16 bits. I guess they added much stronger checksums.

Henry
[ October 28, 2006: Message edited by: Henry Wong ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: long Value to byte Array of size 4
 
Similar Threads
Printing out Ascii charactors
Bit Wise and Bit Shift error
stupid question
download stream gets broken
can we store an integer in a byte array