40

I would like to view binary (or hex) representations of integers in my watch window in Eclipse when debugging. How do I do this?

Lii
  • 11,553
  • 8
  • 64
  • 88
jcartano
  • 401
  • 1
  • 4
  • 3

2 Answers2

49

You can do this from Window->Preferences.

For primitives, browse to Java->Debug->Primitive Display Options

Here there is a checkbox for 'Display Hexadecimal values'. Check this, and you will see both decimal & hexadecimal representations for primitives in the 'value' column under Variables view when debugging.

For objects (Integer, Long, etc), browse to Java->Debug->Detail Formatters

For each type you care about, create a detail formatter that formats the value how you like. For java.lang.Integer, you could use the detail formatter: Integer.toHexString(this)

Make sure your detail formatter is enabled, and you should see the hexadecimal representation in the 'details' area when you select a variable from the Variables view.

Joshua McKinnon
  • 24,489
  • 11
  • 57
  • 63
  • 1
    Doesn't seem to work for arrays. I'm trying to display an int[] as hex, but changing the primitives doesn't change it and I don't know what class to specify for eclipse to translate. – mjaggard Nov 26 '12 at 09:46
  • 1
    It doesn't work for an unexpanded array in the Variables view, but when expanded it should still show the hex values of the array elements. [screenshot](http://i926.photobucket.com/albums/ad105/onelin/java-variables-int-array.png). I can't think of a way you'd specify an array under detail formatters either... – Joshua McKinnon Nov 27 '12 at 03:13
13

Your best option is probably to open the Expressions view (Window -> Show View -> Expressions), right click, choose "Add Watch Expression" and then enter Integer.toBinaryString(yourInt) or Integer.toHexString(yourInt), and click ok.

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
aioobe
  • 413,195
  • 112
  • 811
  • 826