This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Eclipse/Java bytes to hex string not converted correctly  RSS feed

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have filled a byte buffer with float data. The filling was done by reading from a file.



I wanted to see hex string for each byte, so I used get(index, 16) method.



Some of the resulting strings are puzzling.

In one instance, I got these hex strings:

for b.get(0), 16), b.get(1), 16), b.get(2), 16) and b.get(3), 16) respectively.

When I read the original file using a hex editor, I see


Another example:
My java codes give "-4e 4d 60" 4e whereas the hex editor shows "b2 4d 60 4e".

The first byte is not interpreted correctly.

What could be wrong?

 
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a clue - "signed integer"
.
.

Try (b.get(0) && 0xFF ) to remove the sign.

Bill
 
author
Sheriff
Posts: 23590
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tien Shan wrote:
In one instance, I got these hex strings:

for b.get(0), 16), b.get(1), 16), b.get(2), 16) and b.get(3), 16) respectively.

When I read the original file using a hex editor, I see


What could be wrong?




Java bytes are signed. When they get cast to ints, they get sign extended. Your hex editor is treating the bytes as unsigned. Anyway, 0xFC as an unsigned representation, is the same as -0x04 as a signed representation, so there is nothing wrong -- except the printout.

Henry
 
Henry Wong
author
Sheriff
Posts: 23590
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

William Brogden wrote:Here is a clue - "signed integer"
.
.

Try (b.get(0) && 0xFF ) to remove the sign.

Bill




I think you meant to use the bitwise AND and not the logical AND.

Henry
 
Tien Shan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi William and Henry,
It did it! Signed integer, huh? Learnt one more thing.
Yes-- b.get(0) & 0xFF, 16).
Made my day. Thank you so much.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!