By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,064 Members | 1,449 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,064 IT Pros & Developers. It's quick & easy.

Converting to binary

P: 60
Hi Guys,

i want to convert a decimal value to binary. There is a function that does that in stdlib.h called itoa() but is it not standard hence not all compilers recognize it. Is there any other function (or series of funtions) that I can use. I can write mine from the scratch but I would like to know, first, if there is a function that does that.
Jun 19 '07 #1
Share this Question
Share on Google+
7 Replies


P: 23
Hi,
as far as I am concern, itoa doesnt convert from decimal to binary. It converts from integer to string(ascii, thats why i(integer) to a(ascii)). For example, if you have an integer 1225, then itoa will convert it into "1225".

You will be better of writing your own code for decimal to binary conversion:)

Did you really meant to convert from decimal to binary or you wanted a standard function to convert from integer to string (similar to itoa may be)?

Thanks,
Sorower
Jun 19 '07 #2

archonmagnus
100+
P: 113
Hi Guys,

i want to convert a decimal value to binary. There is a function that does that in stdlib.h called itoa() but is it not standard hence not all compilers recognize it. Is there any other function (or series of funtions) that I can use. I can write mine from the scratch but I would like to know, first, if there is a function that does that.
There aren't any standard functions to convert decimal to binary. It isn't that hard to write a conversion function. Strangely, it would be less time consuming to write the function from scratch than to search the web for accessing a (non-existant) built-in conversion. :p
Jun 19 '07 #3

ilikepython
Expert 100+
P: 844
Hi,
as far as I am concern, itoa doesnt convert from decimal to binary. It converts from integer to string(ascii, thats why i(integer) to a(ascii)). For example, if you have an integer 1225, then itoa will convert it into "1225".

You will be better of writing your own code for decimal to binary conversion:)

Did you really meant to convert from decimal to binary or you wanted a standard function to convert from integer to string (similar to itoa may be)?

Thanks,
Sorower
Actually, itoa()'s third arguement, is the base. So to convert from decimal to binary you would do this:
Expand|Select|Wrap|Line Numbers
  1. int num = 43;
  2. char bin_string[8];
  3.  
  4. itoa(num, bin_string, 2);
  5.  
  6. cout << bin_string << endl;    // outputs "101011"
  7.  
Jun 19 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
Here's thing same thing written in C++:

Expand|Select|Wrap|Line Numbers
  1. int num = 43;
  2. bitset<8> bin_string(num);    
  3. string str;
  4. str = bin_string.to_string();
  5. cout << str << endl;
  6.  
Jun 20 '07 #5

archonmagnus
100+
P: 113
Here's thing same thing written in C++:

Expand|Select|Wrap|Line Numbers
  1. int num = 43;
  2. bitset<8> bin_string(num);    
  3. string str;
  4. str = bin_string.to_string();
  5. cout << str << endl;
  6.  
Well... I guess one learns something new every day. ;)
Jun 20 '07 #6

weaknessforcats
Expert Mod 5K+
P: 9,197
Well... I guess one learns something new every day. ;)
I'd love to take credit for this but it was AdrianH that put me onto it.
Jun 21 '07 #7

archonmagnus
100+
P: 113
The way that I was thinking to do it (which is hardly as elegant as weaknessforcat's above) is with the following code:

Expand|Select|Wrap|Line Numbers
  1. unsigned char bitMask = 0x80;
  2. unsigned short input = 43;  // outputs "00101011"
  3.  
  4. for (int i = 0; i < 8; i++)
  5. {
  6.     if (input & bitMask)
  7.         cout<<"1";
  8.     else
  9.         cout<<"0";
  10.  
  11.     bitMask =>> 1;
  12. }
  13.  
Jun 21 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.