Binary number question

What is the formula for converting base 10 numbers to base 2 numbers?  Also is their a predefined header that will do this work for me?  Hopefully a header that accounts for signed numbers.
hawhiteAsked:
Who is Participating?
 
azamiConnect With a Mentor Commented:
From binary to decimal - start at end of string (left) - that is your 1's place, the character to the right of that is the 2's place, then 4's, 8's, etc. (2^x where x is 0 for the leftmost character, 1 for the next, 2, 3,...).

int result = 0;
int pos = strlen(input) - 1;
for (x=0; pos >= 0; x++, pos--)
{
    if (input[pos] == '1')
        result += (2^x);
}

Of course that assumes no error correction.  You can convert the result to a string with sprintf().

Going the other way, the algorithm is to divide into successively smaller powers of two, each iteration working on the remainder from the last iteration.  You can start at any power of two that is higher than your number, or the highest power of two less than your input - any lower and you'll lose data.  So, with an input of 100, you want to do something like:
100/128 = 0 R100
100/64 = 1 R36
36/32 = 1 R4
4/16 = 0 R4
4/8 = 0 R4
4/4 = 1 R0
0/2 = 0 R0
0/1 = 0 R0
String the results together: decimal 100 is 01100100 in binary.

Those are the algorithms if you really want to implement them - but it's much easier to simply use strtol() (which will convert a string to an internal representation as a long, and has a parameter for the radix) and ltoa() (which converts the internal long representation to a string, and also takes a radix parameter).  So, to convert from a binary string to a decimal string, use strtol() with radix 2, and ltoa() the result with radix 10.  Swap the radices to go the other way.

0
 
lif081199Commented:
From the C documentation :

The itoa function converts the binary integer value into the equivalent string in base radix notation storing the result in the character array pointed to by buffer.  A null character is appended to the result.  The size of buffer must be at least (8 * sizeof(int) + 1) bytes when converting values in base 2.  That makes the size 17 bytes on 16-bit machines, and 33 bytes on 32-bit machines.  The value of radix must satisfy the condition:

     
     2 <= radix <= 36

If radix is 10 and value is negative, then a minus sign is prepended to the result.


Example:

#include <stdio.h>
#include <stdlib.h>

void main()
  {
    char buffer[20];
    int base;

    for( base = 2; base <= 16; base = base + 2 )
      printf( "%2d %s\n", base,
              itoa( 12765, buffer, base ) );
  }

produces the following:

 2 11000111011101
 4 3013131
 6 135033
 8 30735
10 12765
12 7479
14 491b
16 31dd
0
 
hawhiteAuthor Commented:
I have a number 000000001100 (binary) and I would like to convert it to its decimal equivalent, and vice versa.

Therefore I would like to convert 12 (decimal - base 10) to 000000001100 (binary).

What is the formula to do this and is their any code (predefined functions) to do this for me?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
lif081199Commented:
How is stored your 000000001100 value ? In a chars array ?
0
 
hawhiteAuthor Commented:
It is stored in a character array.
0
 
ozoCommented:
strtol
0
All Courses

From novice to tech pro — start learning today.