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?
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 ]
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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: Dumping the bit values of a primitive type