Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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: 23508
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: 23508
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.
 
Get me the mayor's office! I need to tell him about this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!