Discussion:
Quick printf binary number question
(too old to reply)
Jack
2010-02-10 13:00:28 UTC
Permalink
Hello,
I am wondering how to display a binary number with printf. (00000001b)

The specifier should look something like this
printf ("%b\n", a);

Have taken a look at my reference but did not find it
Thanks in advance
Jack
Igor Tandetnik
2010-02-10 13:22:14 UTC
Permalink
Post by Jack
I am wondering how to display a binary number with printf. (00000001b)
printf won't help you with that. In fact, to the best of my knowledge, there is nothing in C or C++ standard library that could produce a binary representation of an integer. You'll have to roll your own.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Victor Bazarov
2010-02-10 13:51:48 UTC
Permalink
Post by Igor Tandetnik
Post by Jack
I am wondering how to display a binary number with printf.
(00000001b)
printf won't help you with that. In fact, to the best of my
knowledge, there is nothing in C or C++ standard library that could
produce a binary representation of an integer. You'll have to roll
your own.
Actually the std::bitset has the to_string function. It can help.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Tom Walker
2010-02-11 03:57:21 UTC
Permalink
Post by Victor Bazarov
Post by Igor Tandetnik
Post by Jack
I am wondering how to display a binary number with printf.
(00000001b)
printf won't help you with that. In fact, to the best of my
knowledge, there is nothing in C or C++ standard library that could
produce a binary representation of an integer. You'll have to roll
your own.
Actually the std::bitset has the to_string function. It can help.
Also, the _itoa and _itow family of functions will produce a binary string
when 2 is used as the radix.

http://msdn.microsoft.com/en-us/library/yakksftt(VS.80).aspx
http://msdn.microsoft.com/en-us/library/0we9x30h(VS.80).aspx

Loading...