Reputation: 1559
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?
Upvotes: 13
Views: 14331
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):
Upvotes: 0
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:
Here is a direct link to the complete list of format specifiers.
Upvotes: 5
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 :
The function that in my code is:
public static string ToBinaryString(uint num)
{
return Convert.ToString(num, 2).PadLeft(32, '0');
}
Upvotes: 2