Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Convert hex to decimal value

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
John McDonald
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeff,
Thanks for a quick response. Is the return value a decimal value?

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

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

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

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

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!
 
What are you doing in my house? Get 'em tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic