2

Possible Duplicate:
Converting string of 1s and 0s into binary value

Lets say that I have string which contains 1024 characters (represents 0 and 1). I want to present it as an number in decimal base (also as a string). The tricky part is that i have to do it in C/C++ without 3rd part libraries. Any clues?

13
  • Sounds like a homework question to me... Commented Oct 31, 2012 at 20:18
  • Are you looking for an algorithm or for a standard function that might do this for you? Commented Oct 31, 2012 at 20:18
  • 2
    Is it C or C++? The answers would be very different. Commented Oct 31, 2012 at 20:18
  • 1
    @FlorinStingaciu definitely not impossible Commented Oct 31, 2012 at 20:39
  • 2
    @ThomasMatthews : The important part here is the input string size. The question you link to isn't going to work with 1024 characters. Commented Oct 31, 2012 at 21:08

3 Answers 3

5

There are probably more efficient ways, but I'd have an array of decimal digits, and implement a 'left-shift' function on it which runs from the least significant digit, doubling them and carrying over into the next.

It's then just a job of reading your binary data in one bit at a time and 'left-shifting' the decimal array and 'OR'ing in the binary digit as required.

Just iterate through the decimal digits to print out the answer.

void outputAsDecimal(char *binary)
{
   char digits[1000]; // arbitrary size for now

   for (int i=0; i< 1000; ++i)
     digits[i] = 0;

   while (*binary != 0)
   {

   // shift the digits, with carry
     int carry = 0;

     for (int i = 0; i< 1000; ++i)
     {
       int d = digits[i] *2 + carry;
       carry = d > 9;
       digits[i] = d % 10;
     }

   // or in the new bit
     if (*binary++ == '1') 
       digits[0] |= 1;
   }

    // output with leading zeroes!
    for (int i = 999; i >=0; --i)
    {
      putchar(digits[i] + '0'); // convert to ascii
    }
}

See it running here: http://ideone.com/CibAfw

Sign up to request clarification or add additional context in comments.

9 Comments

That's cool apart from that it's not answer for my question. I will do what you have said, but first need to make a conversion.
@Kostek What is the conversion you have a problem with?
@DanF I think he means he means converting a 0 or 1 from a char to an int/bool
Problem is that he have a point if I have them in decimal base. I don't. I have it in base 2. He don't give any solution, he just said it would be esier if blablabla... I know it, but I can't do nothing about it. I have them in base 2 and want to move them to base 10.
@Kostek, no, you misunderstand. The process of shifting the decimals IS converting your binary input into base 10.
|
1

Edit: Aha! I have just noticed the 1024 requirement. This makes it more complicated, but the idea remains the same. Instead of just having an int number, you need int number[32] (or long number[16], what have you.)

The math at the borders is annoying, but not impossible. Let me know if you can't figure it out.

This works for me. Decomposition and supporting values greater than offered in an (int) is left as an exercise to the reader...

#include <stdio.h> // only to print - not needed in computation
int main(int argc, char *argv[]) {
  printf("Converting: %s\n", argv[1]);
  int number = 0x0;
  char * binaryString = argv[1];
  int index = 0;
  int asciiZero = '0';
  char curr = binaryString[index];
  while(curr != '\0') {
    number = (number << 1) | (curr - asciiZero);
    index++;
    curr = binaryString[index];
  }

  printf("As number: %d\n", number);

  int MAX_DIGITS = 10; //adjust accordingly...
  char buffer[MAX_DIGITS];
  index = 0;
  while(number > 0) {
    buffer[index] = ((char) number % 10) + asciiZero;
    index++;
    number = number / 10;
  }
  buffer[index] = '\0';

  printf("As string: %s\n", buffer);
 }

If you want to support more than offered in the primitives available to you, you can make a struct containing multiple ints/longs/etc.

Comments

-1

You can't represent a 1024 bit number as a decimal. The closest you could get is a floating point approximation.

EDIT:

A thought occurs. Put the numbers in a stack and count them that way. Still have to figure out how to add them together though. You're going to have to implement your own BigInt library if you want to do this, there's not a "trivial" way, as far as I can imagine.

Something like this is a good starting point:

http://gmplib.org/

10 Comments

Yes you can. He just wants to print the result, not to store it as an integer variable.
@amaurea how do you propose he prints a result if he has no way of keeping track of the total?
@Pheonixblade9 he just has to store it in something other than an int. how do you think a pocket calculator works?
@Roddy a pocket calculator works by storing 0's and 1's in registers on the chip.
It seems like you're thinking of a built-in integer variable when you say a decimal. That is not what was asked for - he just wants to print out the result as a string. In fact, the string (i.e. a character buffer) could easily be your work array. Just implement decimal math on character arrays, with each character representing a digit, and then print out the resulting string.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.