• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dumping the bit values of a primitive type

 
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic