This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dumping the bit values of a primitive type

 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do we dump the bit value of a primitive type? For example, given a character '\u23ab' and we want to get "23 ab" printed on the screen. Also, is it possible to dump the internal floating point representation of a double/float type as a sequence of bytes?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easy way is with Integer.toHexString() or Long.toHexString(). Unfortunately these don't bad the result with zeros, which is something you probably want to do if you're going to be writing multiple hex values one after another. And annoyingly, JDK 5's Formatter class doesn't seem to support zero-padding a hex string either. Not the way I'd want, anyway. You can do this:

but that gives spaces rather than zeros. I think they should support this format:

but apparently they don't. (Using a 0 like that works for decimal formatting though.)

Anyway, for the kind of formatting you're tryign to do, you will probably need to write your own code. Or you can try googling "java hexutil" "java hexutils", and "java hexdump" - you will find many examples out there. None of the ones I looked at seemed to have as many formatting options as I thought they should have though. Simple things like inserting spaces every n letters for readability. However I'm sure there are many I didn't look at.

[Alec]: Also, is it possible to dump the internal floating point representation of a double/float type as a sequence of bytes?

You should probably take a look at Double.doubleToLongBits() and Double.doubleToRawLongBits(). Or in JDK 5, format with a %a format specifier:

[ February 04, 2006: Message edited by: Jim Yingst ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.format("%08h%n", 123);

Jim, the format code for hexadecimal is x or X, not h. And this pads it with zeroes as you'd expect:

Output:

0000007B

To dump the 64-bit representation of a double in hex format:

[ February 04, 2006: Message edited by: Jesper de Jong ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, thank you. I was almost certain I'd seen that work previously, but it wasn't happening. In fact Formatter has h and H format codes as well as x and X. Obviously the latter are what I needed, while the former are what I found first this time. I overlooked the bit about how h apparently stands for both "hex" and "hash" at the same time in this case. Conveniently for ints, the hash is the same as the int value, so h looks deceptively like it's just doing a hex conversion.
[ February 04, 2006: Message edited by: Jim Yingst ]
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic