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?

Community
  • 1
  • 1
abc
  • 2,371
  • 3
  • 25
  • 36
  • Sounds like a homework question to me... – Dan F Oct 31 '12 at 20:18
  • Are you looking for an algorithm or for a standard function that might do this for you? – Paul Manta Oct 31 '12 at 20:18
  • 2
    Is it C or C++? The answers would be very different. – Sergey Kalinichenko Oct 31 '12 at 20:18
  • @Dan F sorry, but you're wrong – abc Oct 31 '12 at 20:23
  • @Paul Manta algorithm, i think about that a bit. I can think of naive approach which is implements multiplication (therefor power) and addition on strings, so i can do it exact way i will do it for int32, but i have been wondering is that worthy if there's some "magic algorithm" – abc Oct 31 '12 at 20:24
  • @dasblinkenlight no matter, give any of them or both. – abc Oct 31 '12 at 20:24
  • The more I think about this, the more I think it's impossible. Even if you do implement some sort of string addition function you're still going to need to know what 2^1024, 2^2023 ...etc equal to. – Florin Stingaciu Oct 31 '12 at 20:34
  • 1
    @FlorinStingaciu definitely not impossible – Dan F Oct 31 '12 at 20:39
  • @DanF Maybe I chose the wrong word. Maybe not worth the effort would be better. I'm sure some googling around will most likely return some solutions. – Florin Stingaciu Oct 31 '12 at 20:41
  • just to be sure, STL is 3rd party lib for you? – relaxxx Oct 31 '12 at 20:45
  • Are you saying the string is in binary and you want to convert it to a normal base 10 string? – hookenz Oct 31 '12 at 20:58
  • 2
    @ThomasMatthews : The important part here is the input string size. The question you link to isn't going to work with 1024 characters. – Roddy Oct 31 '12 at 21:08
  • @Thomas Matthews, and You have read it? That topic doesn't help at all in my case, nvm... anyway thanks for minus just because... – abc Oct 31 '12 at 21:09

3 Answers3

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

Roddy
  • 66,617
  • 42
  • 165
  • 277
  • 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. – abc Oct 31 '12 at 20:39
  • @Kostek What is the conversion you have a problem with? – Dan F Oct 31 '12 at 20:39
  • @DanF I think he means he means converting a 0 or 1 from a char to an int/bool – Codeman Oct 31 '12 at 20:40
  • 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. – abc Oct 31 '12 at 20:45
  • @Kostek, no, you misunderstand. The process of shifting the decimals IS converting your binary input into base 10. – Roddy Oct 31 '12 at 20:47
  • @Roddy can you give me an example? let's say i have "1...1" {65} <- which means that i have 65 1 bits how to print out "36893488147419103232" using that what you saying about?? – abc Oct 31 '12 at 20:54
  • @Kostek - an untested code sample here now. – Roddy Oct 31 '12 at 20:57
  • @Kostek - tested and running: http://ideone.com/CibAfw – Roddy Oct 31 '12 at 21:05
  • Thank you very much that's exactly what i needed, now i have to understand it. Thanks once again. – abc Oct 31 '12 at 21:07
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.

Austin
  • 1,122
  • 2
  • 10
  • 27
-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/

Codeman
  • 12,157
  • 10
  • 53
  • 91
  • 3
    Yes you can. He just wants to print the result, not to store it as an integer variable. – amaurea Oct 31 '12 at 20:35
  • @amaurea how do you propose he prints a result if he has no way of keeping track of the total? – Codeman Oct 31 '12 at 20:36
  • 1
    @Pheonixblade9 he just has to store it in something other than an int. how do you think a pocket calculator works? – Roddy Oct 31 '12 at 20:37
  • He can keep track of things by using multiple variables. Think of how you would do this by hand. It would be some work, but totally doable. – amaurea Oct 31 '12 at 20:37
  • 2
    @Roddy a pocket calculator works by storing 0's and 1's in registers on the chip. – Codeman Oct 31 '12 at 20:38
  • @amaurea the problem is printing out the total. He would have to implement his own FSM or something in order to get this to work. Which I suppose is possible, but that's not a decimal number. – Codeman Oct 31 '12 at 20:39
  • The reason I say he can't is because my assumption is that he is attempting to store this in a decimal. There is no IEEE representation for a 1024-bit decimal in the C or C++ languages without non-primitives like `BigInteger` – Codeman Oct 31 '12 at 20:42
  • 1
    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. – amaurea Oct 31 '12 at 20:44
  • @amaurea OP seems to think there is a trivial way to do this. There is not, he would have to implement his own BigInt library. – Codeman Oct 31 '12 at 20:45
  • @amaurea do you have any reason to still have the downvote on this answer? – Codeman Oct 31 '12 at 20:53