Stepagrus
Stepagrus

Reputation: 1559

Visual Studio debugger - Displaying integer values in Binary

I'm using Visual Studio 2017 and I need to look at the binary representation of integer variables.

How can this be achieved from the Visual Studio debugger?

Visual Studio Watch Window

Upvotes: 13

Views: 14331

Answers (4)

Ethan Fischer
Ethan Fischer

Reputation: 1489

I wrote an extension method Binary() and then use the Watch window to call it on whatever variable I want to see.

public static class Extensions
{
    public static string Binary(this byte inputByte)
    {
        return $"{Convert.ToString(inputByte, toBase: 2).PadLeft(8, '0')}";
    }
}

Screenshot showing it in action in VSCode (should work in Visual Studio, too):

VSCode screenshot showing Binary() extension method in action

Upvotes: 0

drerD
drerD

Reputation: 689

Type 'var, b' in the watch, for example:

enter image description here

Upvotes: 21

rbento
rbento

Reputation: 11628

According to the Visual Studio debugger documentation:

You can change the format in which a value is displayed in the Watch, Autos, and Locals windows by using format specifiers.

This note on debugging engine updates and compatibility is also worth noting:

When the Visual Studio native debugger changed to a new debugging engine, some new format specifiers were added and some old ones were removed. The older debugger is still used when you do interop (mixed native and managed) debugging with C++/CLI.

Although it mentions it can be applied to Autos and Locals windows, it is unclear how it is done as the variable names cannot be edited in those windows.

A <variable>, <format> syntax may be used in Watch and Immediate windows, like so:

enter image description here

Here is a direct link to the complete list of format specifiers.

Upvotes: 5

Fletcher
Fletcher

Reputation: 420

Right-click the value it’ll show a menu list, but it only give us the option of Hexadecimal Display. To display the variable with binary value in watch window, I suggest you write function to covert it :

enter image description here

The function that in my code is:

public static string ToBinaryString(uint num)
    {
        return Convert.ToString(num, 2).PadLeft(32, '0');
    }

Upvotes: 2

Related Questions