Conversion Problem: Binary to Decimal

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im having a problem when I try to convert binary to decimal!
main.cpp:
<div style="color:black; background-color:white
<pre>#include <Windows.h>
#include <span style="color:#a31515 "resource.h"
#include <span style="color:#a31515 "StringToInt.h"



BOOL CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM);

<span style="color:blue int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevHinstance, LPWSTR cmdLine, <span style="color:blue int cmdShow)
{
DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), NULL, DlgProc);
<span style="color:blue return 0;
}

BOOL CALLBACK DlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
<span style="color:blue int len;
<span style="color:blue int n;
<span style="color:blue char* texto;
<span style="color:blue char buffer[MAX_PATH];

StrToInt* strCls;
strCls = <span style="color:blue new StrToInt;

<span style="color:blue switch (msg)
{
<span style="color:blue case WM_INITDIALOG:
SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM) LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1)));
SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM) LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1)));
<span style="color:blue return TRUE;
<span style="color:blue case WM_CLOSE:

EndDialog(hwnd, IDCANCEL);
<span style="color:blue break;
<span style="color:blue case WM_COMMAND:
<span style="color:blue switch (LOWORD(wParam))
{
<span style="color:blue case IDC_ENC:

len = GetWindowTextLength(GetDlgItem(hwnd, IDC_EDIT2));
texto = (<span style="color:blue char*)GlobalAlloc(GPTR, len + 1 );
GetDlgItemTextA(hwnd, IDC_EDIT2, texto, len + 1);


SetDlgItemTextA(hwnd, IDC_EDIT3, texto);

n = strCls->StrToDec(texto);

_itoa_s(n, buffer, 2);
SetDlgItemTextA(hwnd, IDC_EDIT4, buffer);

n = strCls->BinToDec(buffer);

_itoa_s(n, buffer, 10);
SetDlgItemTextA(hwnd, IDC_EDIT5, buffer);

GlobalFree((HANDLE)texto);
<span style="color:blue break;
}
<span style="color:blue break;

<span style="color:blue default:
<span style="color:blue return FALSE;
}
<span style="color:blue return TRUE;
}



[/code]

<br/>
BinToDec:<br/>
<br/>
<div style="color:black; background-color:white
<pre><span style="color:blue int StrToInt::BinToDec(<span style="color:blue char* text)
{
<span style="color:blue int i, n;
<span style="color:blue int num;

num = 0;
i = strlen(text);


<span style="color:blue for (n=0; n < i; n++)
{

<span style="color:blue if ( text[n] == 1 )
num = ( num * 2 ) + 1;

<span style="color:blue else <span style="color:blue if ( text[n] == 0 )
num = (num * 2 ) + 0;
}
i = 0;
n = 0;

<span style="color:blue return num;
}
[/code]

<br/>
When I put the number 3245043245, after convert from binary to decimal, it should show that number but instead it shows this number : -1049924051<br/>
<br/>
When I put just 9 digits like: 324504324, it works fine! But if I put more than 9, it doenst show the right number!!

<br/>

View the full article
 
Back
Top