Can I use itoa() for converting long long int to a binary string?
I have seen various examples for conversion of int to binary using itoa. Is there a risk of overflow or perhaps loss of precision, if I use long long int?
Edit
Thanks all of you for replying. I achieved what I was trying to do. itoa() was not useful enough, as it does not support long long int. Moreover I can’t use itoa() in gcc as it is not a standard library function.
To convert an integer to a string containing only binary digits, you can do it by checking each bit in the integer with a one-bit mask, and append it to the string.
Something like this:
Edit:
A more general way of doing it might be done with templates:
Note: Both
static_assertandstd::is_integralis part of C++11, but is supported in both Visual C++ 2010 and GCC from at least 4.4.5.