debug : show value as binary data

2440 단어
/// @file		ShowBinaryData.cpp
/// @brief		debug : show value as binary data

#include "stdafx.h"
#include <windows.h>

#define BYTE_BITS	8	///< 1bytes have 8bits
#define STR_ZERO	L"0"
#define STR_ONE		L"1"

/// @fn			fnShowBinaryData
/// @brief		 DWORD 
/// @param		DWORD dwData,  
/// @param		size_t nDataTypeByteCnt,  . 
///				e.g. int => sizeof(int), DWORD => sizeof(DWORD), etc.
/// @return		void
void fnShowBinaryData(DWORD dwData, size_t nDataTypeByteCnt);

int _tmain(int argc, _TCHAR* argv[])
{
	int		iIndex	=	0;

	for(iIndex = 0; iIndex < 0x0f; iIndex++)
	{
		fnShowBinaryData(iIndex, sizeof(int));
	}

	fnShowBinaryData(RAND_MAX, sizeof(int));

	/** run results
	Binary Data [0x0] = 0000,0000,0000,0000,0000,0000,0000,0000
	Binary Data [0x1] = 0000,0000,0000,0000,0000,0000,0000,0001
	Binary Data [0x2] = 0000,0000,0000,0000,0000,0000,0000,0010
	Binary Data [0x3] = 0000,0000,0000,0000,0000,0000,0000,0011
	Binary Data [0x4] = 0000,0000,0000,0000,0000,0000,0000,0100
	Binary Data [0x5] = 0000,0000,0000,0000,0000,0000,0000,0101
	Binary Data [0x6] = 0000,0000,0000,0000,0000,0000,0000,0110
	Binary Data [0x7] = 0000,0000,0000,0000,0000,0000,0000,0111
	Binary Data [0x8] = 0000,0000,0000,0000,0000,0000,0000,1000
	Binary Data [0x9] = 0000,0000,0000,0000,0000,0000,0000,1001
	Binary Data [0xa] = 0000,0000,0000,0000,0000,0000,0000,1010
	Binary Data [0xb] = 0000,0000,0000,0000,0000,0000,0000,1011
	Binary Data [0xc] = 0000,0000,0000,0000,0000,0000,0000,1100
	Binary Data [0xd] = 0000,0000,0000,0000,0000,0000,0000,1101
	Binary Data [0xe] = 0000,0000,0000,0000,0000,0000,0000,1110
	Binary Data [0x7fff] = 0000,0000,0000,0000,0111,1111,1111,1111
	*/

	getchar();
	return 0;
}

void fnShowBinaryData(DWORD dwData, size_t nDataTypeByteCnt)
{
	int		iPos				=	0;
	int		iPosTag				=	0;
	int		iRorBits			=	0;
	int		dwDataRorAfter		=	0;
	int		iDataBitsNeedRor	=	static_cast<int>(BYTE_BITS) * nDataTypeByteCnt - 1;

	_tprintf(L"Binary Data [0x%x] = ", dwData);
	for (iPos = 0; iPos <= iDataBitsNeedRor; iPos++)
	{
		iRorBits = iDataBitsNeedRor - iPos;
		dwDataRorAfter = dwData >> iRorBits;
		_tprintf(L"%s", (dwDataRorAfter & 1) ? STR_ONE : STR_ZERO);

		///  4 ,  
		if ((0 == (++iPosTag % (BYTE_BITS / 2)))
			&& 
			(iPos < iDataBitsNeedRor))
		{
			_tprintf(L",");
		}
	}

	_tprintf(L"
"); }

좋은 웹페이지 즐겨찾기