22

I started to use VS Code on Linux and I'd like to see the hex value of a variable near its value. I need it because the variable is a mask, so it is composed of 0's and 1's on certain positions. I did not manage to find a setting or something like it to add it.

Is it possible? I am using C++ code. I see that the addresses are in hex, so is there a way to see the value of a variable in hex, too?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
sop
  • 3,445
  • 8
  • 41
  • 84

3 Answers3

45

In the watch window, type <variable-name>,h.

picciano
  • 22,341
  • 9
  • 69
  • 82
Kovalex
  • 1,720
  • 2
  • 11
  • 8
  • 4
    In general, you should look for: https://ftp.gnu.org/old-gnu/Manuals/gdb/html_node/gdb_54.html Thus ,t should work but does not for me at least. Please ask on VSCode GitHub forum, perhaps they do not pass this properly to underlying GDB. Of course, you can always switch to debug console in the bottom and type where: -exec p/t . – Kovalex Oct 23 '18 at 20:05
  • Are all format token from Visual Studio compatible with Visual Code (like `,su` ) ? – Sandburg Nov 28 '19 at 11:36
  • Didn't work for me, "Error: Unrecognized format of field" – Alex E Jun 29 '21 at 15:08
  • 1
    It works for C/C++ programs on Win/Linux with GNU. For Python, one should use hex() instead. So it's not universal solution as Visual Code calls debugging backend for it , as far as I remember. Check lldb's CLI syntax to print value in hex and try it instead of 'h' (in GDB, print /h ) – Kovalex Jul 01 '21 at 13:52
2

In the Watch window of VS Code, you can enter the following:

hex(variable name)
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • A screenshot would have been helpful, because it works: https://i.stack.imgur.com/1h7D0.png – Gino Mempin Jul 22 '22 at 10:38
  • 1
    Note that only works if there is an "hex" immutable, global function that encodes the value as string. E.g. in python – G.G Nov 28 '22 at 15:42
1

Adding ,h etc didn't work for me (at least on lldb on mac).

Try casting the variable to the output type you want:

Example for hex:

(void*)test3x1[1]

Example for integer:

(int)test3x1[1]
Alex E
  • 149
  • 9