56

Is VSCode able to display hex values for local variables in debug mode ? I have been searching for a way to make this happen but with no success.

1
  • you can right-click a variable from variables panel to add to watch list, then add ,h; e.g., originalPtr,h within the watch panel Commented Oct 4 at 8:46

11 Answers 11

104

I know this is an old thread, but it was at the top of my Google search so I thought I'd add some new information, which I found in the issues thread linked by Burt_Harris.

You can't seem to change the formatting of values displayed in the Locals pane or in tooltips, but you can force the formatting on variables in the Watch pane by appending ,x to the variable name.

Other formats exist, such as ,b for binary, ,o for octal. I believe it's based on the GDB display modifiers uses (e.g. display/x myVariable)

Suffixes used in VSCode's Watch pane:

Suffixes used in VSCode's Watch pane

Sign up to request clarification or add additional context in comments.

3 Comments

That only works when using GDB, not with Clang
does not work with structs
For Python you can format the value as usual in a print : f"{page:08_x}" where page is the variable to display.
25

It has been a while since last activity on this, but I found by looking through links from this thread on the Cortex-Debug github (Issues) a solution for me. From the (GDB) Debug Console use -exec set output-radix 16 for hex, set it to 10 for decimal.

3 Comments

Thanks! I would just remind beginners that you need to type -exec before the set command: -exec set output-radix 16
This also works in Nordic NCS debug console
FYI, there is a current bug that doing this causes the context menu on locals to stop working - github.com/microsoft/vscode-cpptools/issues/13742
23

This answer applies to GDB debugging.

For an active debug session, type the following in the debug console:

-exec set output-radix 16

To apply this every time the debugger runs, add text "set output-radix 16" in the "setupCommands" section of launch.json

"setupCommands": [
    {
        "description": "Enable pretty-printing for gdb",
        "text": "-enable-pretty-printing",
        "ignoreFailures": true
    },
    { "text": "set output-radix 16" }
]

4 Comments

This helped me to setup my debugger as needed!
Got me invalid command 'settings output-radix’. Has this feature been removed?
Verify in your DEBUG CONSOLE tab that you are running gdb (vs lldb for example).
FYI, there is a current bug that doing this (in console or in setupCommands) causes the context menu on locals to stop working - github.com/microsoft/vscode-cpptools/issues/13742
12

I was looking for the same thing and ended up here, and I saw that the feature request has been denied at this time.

But then it hit me: In the watch window, you can add an expression, and Number have a toString method, where you can choose what radix (2-36) to convert the number to. And it works:

Watch window with .toString

Instead of just watching value, watch value.toString(16) for hex.

I tried to add more methods to the Number prototype in my code (I wanted a grouped display), but unfortunately it is only shown as "[Object object]".

I know that this is not exactly what you where looking for, but it is something that works without any plugins.

1 Comment

Also works nicely for PWSH/DotNET: $xyz.ToString('x')
6

Using GDB, you can cast it to void*: (void*)var, and it will treat it as a pointer, and show it in hex.

Comments

4

You can't currently, but there are feature requests outstanding for this. According to VSCode core developers, this needs to be implemented in the specific debugger extension for the environment you are debugging.

Links to known related debugger extension feature requests listed below:

Vote your preferences by adding thumbs-up to the feature request

1 Comment

Apparently this feature isn't something the main VS Code devs can add. The values are provided by whatever debugging extension is being used, so the issue should be raised with them.
3

Golang

As @some said, you can add an expression in the watch. Here is the expression you would have to add to display a slice of bytes as a hex string:

call hex.EncodeToString(mySliceOfBytes)

Comments

3

If using the CodeLLDB debugger, the display setting for variables can be changed in the VSCode setting Lldb: Display Format. It can be found by typing lldb.displayFormat in the search bar in VSCode settings.

Note that this changes the display setting for all variables.

Picture showing the LLDB Display Format settings

Comments

1

actually you could open debug console and using hex() function to convert values

Comments

0

For those who still debug Python 2 project, you can use the Watch window with following format:

'{:02X}'.format(myVariable)

Comments

-2

enter image description here

make sure uncheck this box

2 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.