Windows API: Convert Decimal Number to Binary
Converts very large decimal numbers to binary ones. Download the source code: WINAPIBINARYCONV.ZIP
#include <windows.h>
#include <tchar.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
unsigned __int64 AlphaToUnsignedInt(TCHAR *sz);
void CopyString(TCHAR* dst, const TCHAR* src);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = _T("BinaryWin");
HWND hwnd;
MSG msg;
WNDCLASSEX wndclass;
wndclass.cbSize = sizeof (wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject (LTGRAY_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
RegisterClassEx (&wndclass);
hwnd = CreateWindowEx (WS_EX_STATICEDGE, // extended window style
szAppName, // window class name
_T("Convert Decimal to Binary"),// window caption
0, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
600, // initial x size
380, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL); // creation parameters
ShowWindow (hwnd, iCmdShow);
UpdateWindow (hwnd);
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
static HWND hwndEdit;
static TCHAR szResult[100];
#define ID_EDIT 123
#define ID_BUTTON 456
#define ID_BTNEXIT 789
switch (iMsg)
{
case WM_CREATE:
hwndEdit = CreateWindowEx(WS_EX_CLIENTEDGE,_T("EDIT"),NULL,WS_CHILD|WS_VISIBLE|ES_NUMBER,
100,100,475,22,hwnd,(HMENU)ID_EDIT,((LPCREATESTRUCT)lParam)->hInstance,NULL);
CreateWindowEx(0,_T("BUTTON"),_T("Convert to Binary"),WS_CHILD|WS_VISIBLE,
400,250,150,25,hwnd,(HMENU)ID_BUTTON,((LPCREATESTRUCT)lParam)->hInstance,NULL);
CreateWindowEx(0,_T("BUTTON"),_T("Exit Program"),WS_CHILD|WS_VISIBLE,
400,275,150,25,hwnd,(HMENU)ID_BTNEXIT,((LPCREATESTRUCT)lParam)->hInstance,NULL);
return 0;
case WM_COMMAND:
if(LOWORD(wParam) == ID_BUTTON && HIWORD(wParam) == BN_CLICKED)
{
unsigned __int64 num;
int i= GetWindowTextLength(hwndEdit);
TCHAR *sz = new TCHAR[i + 1];
GetWindowText(hwndEdit, sz, i + 1);
for(num = AlphaToUnsignedInt(sz), sz = &szResult[98]; num > 0 && sz >= szResult; sz--)
{
*sz = (TCHAR)(num % 2) + '0';
num /= 2;
}
if(sz == szResult && num)
MessageBox(hwnd,_T("Number too great!"),_T("Binary Converter"),0);
else if(sz > szResult)
CopyString(szResult, sz + 1);
GetClientRect(hwnd, &rect);
InvalidateRect(hwnd, &rect, TRUE);
}
else if(LOWORD(wParam) == ID_BTNEXIT && HIWORD(wParam) == BN_CLICKED)
PostQuitMessage(0);
return 0;
case WM_PAINT :
hdc = BeginPaint (hwnd, &ps);
SetBkMode(hdc, TRANSPARENT);
rect.left = 10;
rect.top = 100;
rect.bottom = 122;
rect.right = 90;
DrawText (hdc, _T("Number:"), -1, &rect, DT_SINGLELINE|DT_RIGHT|DT_VCENTER);
GetClientRect(hwnd, &rect);
rect.top = 150;
rect.bottom = 172;
DrawText (hdc, szResult, -1, &rect, DT_SINGLELINE|DT_CENTER|DT_VCENTER);
EndPaint (hwnd, &ps);
return 0;
case WM_DESTROY :
PostQuitMessage (0);
return 0;
}
return DefWindowProc (hwnd, iMsg, wParam, lParam);
}
unsigned __int64 AlphaToUnsignedInt(TCHAR *sz)
{
int c;
unsigned __int64 total;
while (*sz == ' ' || *sz == '\r' || *sz == '\n') ++sz;
total = 0;
while (c = *sz++)
total = 10 * total + (c - '0');
return total;
}
void CopyString(TCHAR* dst, const TCHAR* src)
{
while(*dst++ = *src++);
}