Computing.Net > Forums > Programming > Decimal To Binary

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Decimal To Binary

Reply to Message Icon

Name: Hacker
Date: September 9, 2003 at 08:12:09 Pacific
OS: winxp
CPU/Ram: 128
Comment:

converting binary to decimal numbers
that is all I need i think i did 2 much
----------------
#include
#include
#include
//convert the string to number
//udata is string
//udatalen is length of string
//base is numerical base eg. for decimal it is 10
// for hex it is 16
//largest base supported here is upto 36

int __fastcall StrToNum(const TCHAR *udata, int udatalen, int base)
{
long index;
const TCHAR numdigits[] = TEXT("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
long digitValue = 0;
long RetVal = 0;
TCHAR digits[sizeof(numdigits)+1];
TCHAR *dataVal;
TCHAR data[512] ;
//copy the data to our variable
_tcscpy(data, udata);
//convert it to upper case
_tcsupr(data);
ZeroMemory(digits, sizeof(digits));
//copy the number of digits supported by base in digits
_tcsncpy(digits, numdigits, base);
for(index = 0; index 36 ) return NULL;
ZeroMemory(digits, sizeof(digits));
_tcsncpy(digits, numdigits, base);
while(number)
{
digitValue = number % base;
number = number base;
RetVal[index++] = digits[digitValue];
}
//since string we have got is in reversed format
//eg 100 will be 001 so we have to reverse it
//and put the value in our variable
ZeroMemory(RetData, _tcslen(RetVal)+1);
int i = 0;
for(index = _tcslen(RetVal) - 1; index > -1; index--)
{
//start reversing
RetData[i++] = RetVal[index];
}
//return the result
return RetData;
}

//our main function
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
TCHAR Data[128];
ZeroMemory(Data, sizeof(Data));
//convert a number to string
NumToStr(Data, 1123, 10);
//now again convert string to number and see the result
cout
#include // For File I/O
#include

using namespace std;

bool validate_line(string s);
int bin_to_dec(string s);
void pretty_print_line(string s);

int main(void)
{
ifstream inFile; // Data file

// Promts the user for a file name.
string fileName; // File name
string binary;

cout > fileName;

// Opens the File.
inFile.open(fileName.c_str()); // Changes variable into a C string and opens the file.
if (!inFile.good()) {
cerr >= 1; // Divide multiplying factor by 2 each time
}
return n_retval;
}

void pretty_print_line(string s)
{
// Print the line without the leading spaces
for (int i = 0; i s.size(); i++)
if (s[i] != ' ')
cout s[i];
}
----------------
Hopefully its one of these



Sponsored Link
Ads by Google

Response Number 1
Name: Hacker
Date: September 9, 2003 at 08:23:52 Pacific
Reply:

I got it just didnt think hard enough

#include
#include
#include
void main()
{
clrscr();
int num,i,j,k,a1[30],l;
char a2[30];
cout>num;
for(i=0;num!=0;i++)
{
a1[i]=num%2;
num=num/2;
}
k=i-1;
for(j=0;ji;j++,k--)
{
a2[j]=a1[k]+48;
}
for(l=j;l=30;l++)
{
a2[l]=' ';
}
cout"\n\nThe Binary Number Is : "a2"\n";
getch();
}


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Decimal To Binary

decimal to binary conversion help!! www.computing.net/answers/programming/decimal-to-binary-conversion-help/4822.html

Convert decimal to binary www.computing.net/answers/programming/convert-decimal-to-binary/17714.html

How to convert Ascii to binary www.computing.net/answers/programming/how-to-convert-ascii-to-binary/829.html