wood burning stoves 2.0*
The moose likes Java in General and the fly likes Convert hex to decimal value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Convert hex to decimal value" Watch "Convert hex to decimal value" New topic
Author

Convert hex to decimal value

John McDonald
Ranch Hand

Joined: Jul 01, 2003
Posts: 112
Hi there,
I have a buffer of 4 bytes long. These four bytes together store a hex value of file size. The code below was written is C and it works.

char *ptr;

unsigned long fileSize = (( unsigned char ) * (ptr+24) * 256 * 256 * 256 ) +
(( unsigned char ) * (ptr+23) * 256 * 256 ) +
(( unsigned char ) * (ptr+22) * 256 ) +
( unsigned char ) *( ptr+21);

Now, I rewrite this section of code in Java, but I don't get the correct result. I think the issue java does not has unsigned char. Any input is appreciated. Thank you very much.

public int getFileSize(int offset)
{
byte[] buffer = //reading from InputStream

long fileSizeL;

byte byte24 = buffer[offset + 24];
byte byte23 = buffer[offset + 23];
byte byte22 = buffer[offset + 22];
byte byte21 = buffer[offset + 21];


fileSizeL = (byte24) * (long)(256 * 256 * 256) +
(byte23) * (long)(256 * 256) +
(byte22) * 256 +
(byte21);

return fileSizeL;
}
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
In Java, char is unsigned -- it is essenitally unsigned short, but I think you really mean that byte in Java is signed, which is true, even though a byte that was unsigned would make mor sense for most uses of byte. The fix is simple: to convert a byte to an int value in the range [0,255] (ie, to treat a byte as unsigned) write:So your code could be written:That being said, check out DataInputSteam -- it may eliminate the need for doing this entirely!
[ January 18, 2006: Message edited by: Jeff Albrechtsen ]

There is no emoticon for what I am feeling!
John McDonald
Ranch Hand

Joined: Jul 01, 2003
Posts: 112
Hi Jeff,
Thanks for a quick response. Is the return value a decimal value?

John
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
It's an int. One doesn't speak of an int being a decimal or hex value because that doesn't make any sense. That's referring to the string format of a number. An int is an int.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by John McDonald:
Hi Jeff,
Thanks for a quick response. Is the return value a decimal value?

John


Technically, an int (or any data for that matter) is stored as binary. You should try compiling this code in a simple test framework and check if the int has the excpected decimal value that you would like for various byte[] inputs.

Layne


Java API Documentation
The Java Tutorial
John McDonald
Ranch Hand

Joined: Jul 01, 2003
Posts: 112
int byte24 = 0xff & buffer[offset + 24];
int byte23 = 0xff & buffer[offset + 23];
int byte22 = 0xff & buffer[offset + 22];
int byte21 = 0xff & buffer[offset + 21];

return byte24<<24 | byte23 <<16 | byte22<<8 | byte21;

This code works with no doubt. Could you explain a bit more on this, please? Why don't we do the reverse instead?

return byte21<<24 | byte22 <<16 | byte23<<8 | byte24;

Thanks
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
I arranged the bytes in that order because I was mimicking your code:
Perhaps your question really is: which way are the bytes of a int stored? The answer is that it depends. Java prefers to store hi-order bytes first (big-endian).
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Jeff Albrechtsen:
It's an int. One doesn't speak of an int being a decimal or hex value because that doesn't make any sense. That's referring to the string format of a number. An int is an int.


...unless of course, you are legislating for the safety of hundreds of thousands of Australian road users.
http://tmorris.net/pubs/md5-speed-cameras/ (second last paragraph)

Sorry, I just had to throw that in


Tony Morris
Java Q&A (FAQ, Trivia)
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by John McDonald:
int byte24 = 0xff & buffer[offset + 24];
int byte23 = 0xff & buffer[offset + 23];
int byte22 = 0xff & buffer[offset + 22];
int byte21 = 0xff & buffer[offset + 21];

return byte24<<24 | byte23 <<16 | byte22<<8 | byte21;

This code works with no doubt. Could you explain a bit more on this, please? Why don't we do the reverse instead?

return byte21<<24 | byte22 <<16 | byte23<<8 | byte24;

Thanks


You can concievably use either of these. The order of the bytes depends on whether the value is represented in big-endian or little-endian format. If you are interested, you should google for more information on these.

Layne
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Tony Morris:
...unless of course, you are legislating for the safety of hundreds of thousands of Australian road users.
http://tmorris.net/pubs/md5-speed-cameras/ (second last paragraph)


I knew that sooner or later too much Vegemite could cause some sort of brain damage But then again, it's not like they tried to legislate that PI be 3.2!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Convert hex to decimal value
 
Similar Threads
Why is Java's MD5 different from MySQL MD5?
BigInteger to byte array of size 8 bytes (Unsigned long) in Java
audio visualization graphics
MD5
tripleCut Solitaire Algorithim