PROWAREtech
Windows API: Convert Decimal to Binary
Convert decimal number to binary number; written in C.
Convert very large decimal numbers to binary ones.
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
unsigned long long AlphaToUnsignedInt(WCHAR* sz);
void CopyString(WCHAR* dst, const WCHAR* src);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static const WCHAR szAppName[] = L"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)(COLOR_WINDOW + 1);
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
L"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 WCHAR szResult[100];
#define ID_EDIT 123
#define ID_BUTTON 456
#define ID_BTNEXIT 789
switch (iMsg)
{
case WM_CREATE:
hwndEdit = CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", NULL, WS_CHILD | WS_VISIBLE | ES_NUMBER,
100, 100, 475, 22, hwnd, (HMENU)ID_EDIT, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
CreateWindowEx(0, L"BUTTON", L"Convert to Binary", WS_CHILD | WS_VISIBLE,
400, 250, 150, 25, hwnd, (HMENU)ID_BUTTON, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
CreateWindowEx(0, L"BUTTON", L"Exit Program", WS_CHILD | WS_VISIBLE,
400, 285, 150, 25, hwnd, (HMENU)ID_BTNEXIT, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
return 0;
case WM_CLOSE:
DestroyWindow(hwnd);
return 0;
case WM_COMMAND:
if (LOWORD(wParam) == ID_BUTTON && HIWORD(wParam) == BN_CLICKED)
{
unsigned long long num;
int i = GetWindowTextLength(hwndEdit);
WCHAR* sz = new WCHAR[i + 1];
GetWindowText(hwndEdit, sz, i + 1);
for (num = AlphaToUnsignedInt(sz), sz = &szResult[98]; num > 0 && sz >= szResult; sz--)
{
*sz = (WCHAR)(num & 1) + '0';
num >>= 1;
}
if (sz == szResult && num)
MessageBox(hwnd, L"Number too great!", L"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)
SendMessage(hwnd, WM_CLOSE, 0, 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, L"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 long long AlphaToUnsignedInt(WCHAR* sz)
{
int c;
unsigned long long total;
while (*sz == ' ' || *sz == '\r' || *sz == '\n') ++sz;
total = 0;
while (c = *sz++)
total = 10 * total + (c - '0');
return total;
}
void CopyString(WCHAR* dst, const WCHAR* src)
{
while (*dst++ = *src++);
}
Comment