## Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of posts: 16949

This Forum Only

best way to convert a n-bit binary to decimal string? Posted by ColacX on 2 Sept 2010 at 1:55 PM
best way to convert a n-bit binary to decimal string?

hi i was making a class that can hold and compute very long numbers like n-bit long numbers so if 32-bit int would be too little it would add another 32-bit int in a dynamic linked list and use that for computing, computing the numbers in binary is very easy but converting to a decimalstring is very hard.

i've made a function that can takes 2 decimalstrings and adds them together and returns a summed string for example

sum( "2302030", "3023852" ); will return "5325882"

i was thinking that for a long number that uses over 32-bit the 33-bit could add the string "4294967295" twice onto the lower order number and the 34-bit would add it 4-times and the 35-bit would add it 8-times however i quickly realized that the exponential rise in computation time isnt a good idea, uh so what do i do? i was hoping to make a class that could compute over a 1000-bit numbers. i know that Mathematica and other math programs seem to have no limit to their numbers so i wanted something like that too, but im not sure what to search for.

the problem seemed simple at first but hmm :/

was looking at the bits themselfs and binary 1010 equals decimal 10 so hmm maybee theres a pattern i can use to convert? i cant see it though

thanks for any help
Re: best way to convert a n-bit binary to decimal string? Posted by anthrax11 on 3 Oct 2010 at 5:51 AM
You don't have to convert to binary and back to do addition. It's relatively easy to do it with just the strings. This code adds two numbers together like how you would on paper.
```#include "stdafx.h"
#include <string.h>
#include <stdlib.h>

void reverse_string(char* str)
{
int len = strlen(str);
int i;

for (i=0; i<len/2; i++)
{
char temp = str[i];
str[i] = str[len-i-1];
str[len-i-1] = temp;
}
}

char* sum(char* a, char* b)
{
char* result;

int a_len = strlen(a);
int b_len = strlen(b);

int len = (a_len > b_len) ? a_len : b_len;

result = (char*)malloc(len+2);

int n;
int carry = 0;
for (n=0; n<len; n++)
{
char digit = carry;

if (a_len-n > 0)
digit += a[a_len-n-1];
else
digit += '0';

if (b_len-n > 0)
digit += b[b_len-n-1] - '0';

if (digit > '9')
{
digit -= 10;
carry = 1;
}
else
{
carry = 0;
}

result[n] = digit;
}

if (carry)
{
result[n] = '1';
n++;
}

result[n] = 0;
reverse_string(result);

return result;
}

int _tmain(int argc, _TCHAR* argv[])
{
char* result = sum("2302030", "3023852");
printf(result);
getchar();
}
```

You can find more about these kinds of operations if you Google for "bignum". You should be able to find plenty of code as well.

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic