*
The moose likes Java in General and the fly likes Dumping the bit values of a primitive type 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 "Dumping the bit values of a primitive type" Watch "Dumping the bit values of a primitive type" New topic
Author

Dumping the bit values of a primitive type

Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
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

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dumping the bit values of a primitive type
 
Similar Threads
please explain. char problem.
Math.max(), Math.min()
Widening Conversion
Why the following code fails to compile
valueOf() method