Posts tagged visual studio debugger
How to Watch Variables in Binary by using Visual Studio Debugger?
2![]()
In visual studio you can watch variable values in different formats. For instance, for viewing in hex add this to watch window – var,x and for octal – var,o. But its a pity that visual studio doesn’t support displaying variables in binaries. So how can you watch the value of a variable in binary, with debugger?

Image Courtesy – Pixdaus.
![]()
Well, we have to utilize the special feature of visual studio debugger. Basically the watch window is not just a tool to display the variable value. It can evaluate and execute small code snippets as well. If you add a function call to the watch window, that function will be called when debugger refreshes the watch values. So, all you have to do is – Add a global function which accepts integer value, converts it to binary string then display it to output window. Have a look at the function.
// Global function which converts integer to binary
// and dump to the output window.
void DumpBinary( DWORD Value )
{
// Buffer to hold the converted string.
TCHAR Buffer[255] = { 0 };
// Convert the value to binary string.
_itot( Value, Buffer, 2 );
// Display to output window.
CString csMessage;
csMessage.Format( _T("\n%d in binary: %s"), Value, Buffer );
OutputDebugString( csMessage );
}
Now if you want to convert the 100 to binary, break at some location and just add DumpBinary(100) to watch window and check the output in the output window. See the screenshot below.

![]()
Just now I realized – how powerful the Visual Studio Debugger is. Hats off to Visual Studio Team!
Well, please note that in visual studio 2008 and may be in siblings, When you add this to watch window, its possible to see this error – “CXX0001: Error: error attempting to execute user function”. In that case just click the “Evaluate button” which appears next to it and the expression will be re-evaluate.
![]()
Targeted Audiance – Advanced.
Be careful while watching variables with Visual Studio Debugger's tooltip.
0![]()
We used to watch variables values by hovering mouse over them. So visual studio will show the variable value as tooltip. But beware, pitfalls are waiting for you, especially while watching structures.
![]()
If you just hover the mouse over a variable, Visual Studio will pick the variable name just below and shows its value as tooltip. But it can be dangerous. For instance, assume you’ve a structure, and local variable with name same as structure member. Check the code snippet.
// Some structure.
struct Drive
{
BOOL Present;
};
void Function()
{
// Local variable with same name
// as structure member.
BOOL Present = FALSE;
// Structure with variable
Drive CdDrive = { 0 };
// If you watch the var by mouse hover,
// the value displayed will be that of
// local variable.
CdDrive.Present = TRUE;
}
If you just mouse hover on the structure member variable, you’re going to deep trouble. The value displayed will be wrong. Since debugger choose just the variable name under the mouse, if a local variable of same name exists, the value of local variable will be displayed. Enough and more to get mis-guided. Have a look at the screenshot.

So always watch structure members by selecting the full variable name. See following screenshot.

![]()
Silly stuff. But it can grab a lot of time by blaming the compiler and rebuilding source again and again as I did. ![]()
![]()
Targeted Audience – Intermediate.