in a book i saw that there is function built into c that displays the binary value of a number, it looks something link this
showbits(int);
but when i tried it, didnt work, do you know which header file i have to include to enable this?
Printable View
in a book i saw that there is function built into c that displays the binary value of a number, it looks something link this
showbits(int);
but when i tried it, didnt work, do you know which header file i have to include to enable this?
I have never heard of this function 'showbits', but from its name and the sintax I believe that there might be a workaround, or another function that does the same thing as this function. I know that the function '_itoa(string, number, base)' in MSVC++ 6, can be used to display the binary value of a integer in a string. I don't know if this will work in other compliers but here is an example of the usage of _atoi for MSVC++.
The output is as follows.Code:#include <cstdlib>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
char buffer[20];
int i = 3445;
_itoa( i, buffer, 10 );
cout << "String of integer " << i << " (radix 10): " << buffer << endl;
_itoa( i, buffer, 16 );
cout << "String of integer " << i << " (radix 16): " << buffer << endl;
_itoa( i, buffer, 2 );
cout << "String of integer " << i << " (radix 2): " << buffer << endl;
return 0;
}
-JLBsheckyCode:String of integer 3445 (radix 10): 3445
String of integer 3445 (radix 16): d75
String of integer 3445 (radix 2): 110101110101
Which might be fine if they were using C++. However, this is the C board. :DQuote:
Originally posted by JLBSchreck
I know that the function '_itoa(string, number, base)' in MSVC++ 6, can be used to display the binary value of a integer in a string. I don't know if this will work in other compliers but here is an example of the usage of _atoi for MSVC++.
-JLBsheckyCode:snip c++ code
Quzah.
Replace all cout statements with fitting printf statements and there you are. _itoa is plain C.Quote:
Originally posted by quzah
Which might be fine if they were using C++. However, this is the C board. :D
Quzah.
But this is the cross-platform C forum, not the MS forum ;)
Not really. _itoa is Microsoft. the underscore is the clue. Just this alone tells me that itoa() is not a standard function.Quote:
Originally posted by CornedBee
Replace all cout statements with fitting printf statements and there you are. _itoa is plain C.
ANSI C only defines ato?() functions, not the other way around. MS usually puts an underscore in front of functions that are not ANSI Standard, whereas Borland does not.
Many compilers though have defined ?toa() functions, but you can't rely on them for portability.
Plain C != Standard C.Quote:
Not really.
This is why I posted this part:
Quote:
But this is the cross-platform C forum, not the MS forum.