435,064 Members | 1,449 Online + 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
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

 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

 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 int num = 43; char bin_string;   itoa(num, bin_string, 2);   cout << bin_string << endl;    // outputs "101011"   Jun 19 '07 #4

 Expert Mod 5K+ P: 9,197 Here's thing same thing written in C++: Expand|Select|Wrap|Line Numbers int num = 43; bitset<8> bin_string(num);     string str; str = bin_string.to_string(); cout << str << endl;   Jun 20 '07 #5

 100+ P: 113 Here's thing same thing written in C++: Expand|Select|Wrap|Line Numbers int num = 43; bitset<8> bin_string(num);     string str; str = bin_string.to_string(); cout << str << endl;   Well... I guess one learns something new every day. ;) Jun 20 '07 #6

 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

 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 unsigned char bitMask = 0x80; unsigned short input = 43;  // outputs "00101011"   for (int i = 0; i < 8; i++) {     if (input & bitMask)         cout<<"1";     else         cout<<"0";       bitMask =>> 1; }   Jun 21 '07 #8 