Ajay Thakur February 2016

How to convert an array of decimals to an array of binary numbers?

*Array i have is

int array{9} = { 2 ,2, 10 , 3 , 1, 15, 12, 6 ,1};

I am trying to covert it into an array of binary, which should be like

int array2[35]= {0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0,0, 0, 1, 1, 0, 0, 0, 1};

Code i am using is:

double decimal2Binary(long m)
{
    int remainder;
    long binary = 0, i = 1;
    while(m != 0)
    {
        remainder = m%2;
        m = m/2;
        binary= binary + (remainder*i);
        i = i*10;
    }
    return binary;
}

The problem is that , when I am converting 2 to binary ,the output is 10. But i want it to be 0010 and similarly for 7 it is giving 111 in place of 0111. What else i need to do. Please help*

Answers


OldAmigan February 2016

If I understand your code, from an int=7 you want an int=111 (100+10+1) that when printed shows as 0111. This is a formatting problem, not a conversion, have a look at printf() how to add leading zeros up to 4 digits.


AchmadJP February 2016

You want it to be exact 4 binary digit?

double decimal2Binary(long m)
{
int remainder;
long binary = 0, i = 1;
while(m != 0)
{
    remainder = m%2;
    m = m/2;
    binary= binary + (remainder*i);
    i = i*10;
}
return binary;
}

This function is great, but as you know you used long and a while function. So how can you get exact 4 digit binary? Use string

Warning, converting this function to 4 digit string result in function max procesing to 1111 -> 15

string decimal2Binary(long m)
{
    int remainder;
    string binary = "";
    while(m != 0)
    {
        remainder = m%2;
        m = m/2;
        if(remainder == 0){
            binary = '0' + binary;
        }
        else{
            binary = '1' + binary;
        }
    }
    while(binary.size() != 4){
        binary = '0' + binary;
    }
    return binary;
}


Marinos K February 2016

0010 is not a valid number in C++. Then there are many ways to do what you want depending on how you will use these binary numbers.

a. use an std::string (or an array of c-style char*) and complete the missing 0s iteratively. You have to first convert your numbers into a string tokens (maybe using boost::lexical_cast or simply using an istringstream), put them into a std::string and then do something like:

for (int i=mystring.size(); i<4; i++) mysting.push_front("0");

b. You can use a 2-dimensional array with each binary number been represented as a int[4].

c. Use std::bitset (http://www.cplusplus.com/reference/bitset/bitset/) You could either have an array (or std::vector) of std::bites<4> - each for a 4bit binary number - or use directly a std::bites<35> (in your example) and make sure you add the missing 0s yourself wherever appropriate.

I'd definitely recommend that you use some kind of dedicated type for your binary numbers (either a std::bites<4> or a char[4] or an int[4] or even a bool[4] or custom Struct) and then use arrays of this type. Such code is better to maintain and better to read.


kwarnke February 2016

It depends if you are using the C printf()function or C++ iostreams for the output.

#include <stdio.h>  // printf()
#include <iostream> // cout, endl
#include <iomanip>  // setw(), setfill()

int main(void) 
{
    double const d = 10;

    printf("%04.0f\n", d); 
    std::cout << std::setw(4) << std::setfill('0') << d <<std::endl;

    return 0;
}

For both the output is 0010.


Lundin February 2016

Everything inside your computer is already in binary format, so there is no need to convert anything.

This is how you print a number on the screen with binary representation:

#include <stdio.h>
#include <stdint.h>

void print_bin (size_t items, const uint8_t array[items])
{
  for(size_t i=0; i<items; i++)
  {
    for(uint8_t mask=0x80; mask!=0; mask>>=1)
    {
      printf(array[i] & mask ? "1" : "0");
    }
    printf("\n");
  }
}

int main (void)
{
  uint8_t array[9] = {2, 2, 10, 3, 1, 15, 12, 6, 1};
  print_bin(sizeof(array)/sizeof(*array), array);
}

This example can be easily modified to create your peculiar "BCD-ish" array2 instead of printing.

Post Status

Asked in February 2016
Viewed 1,687 times
Voted 4
Answered 5 times

Search




Leave an answer