Convert integer value to binary string using itoa in C / C ++
Can it be used itoa()
to convert long long int to binary string? I've seen various examples of converting int to binary using itoa
. Is there any risk of overflow or perhaps loss of precision if I use long long int.
Edit - Thank you all for your answer. I achieved what I tried. itoa () wasn't helpful enough as it doesn't support long long int.Moreover. I cannot use itoa () in gcc as it is not a standard library function.
source to share
To convert an integer to a string containing only binary digits, you can do so by inspecting each bit in integer form with a one-bit mask and adding it to the string.
Something like that:
std::string convert_to_binary_string(const unsigned long long int value,
bool skip_leading_zeroes = false)
{
std::string str;
bool found_first_one = false;
const int bits = sizeof(unsigned long long) * 8; // Number of bits in the type
for (int current_bit = bits - 1; current_bit >= 0; current_bit--)
{
if ((value & (1ULL << current_bit)) != 0)
{
if (!found_first_one)
found_first_one = true;
str += '1';
}
else
{
if (!skip_leading_zeroes || found_first_one)
str += '0';
}
}
return str;
}
Edit:
A more general way to do this can be done with templates:
#include <type_traits>
#include <cassert>
template<typename T>
std::string convert_to_binary_string(const T value, bool skip_leading_zeroes = false)
{
// Make sure the type is an integer
static_assert(std::is_integral<T>::value, "Not integral type");
std::string str;
bool found_first_one = false;
const int bits = sizeof(T) * 8; // Number of bits in the type
for (int current_bit = bits - 1; current_bit >= 0; current_bit--)
{
if ((value & (1ULL << current_bit)) != 0)
{
if (!found_first_one)
found_first_one = true;
str += '1';
}
else
{
if (!skip_leading_zeroes || found_first_one)
str += '0';
}
}
return str;
}
Note. Both static_assert
and std::is_integral
are part of the C ++ 11, but are supported in Visual C ++ 2010 and in the GCC, at least 4.4.5.
source to share
Yes, you can. Once you've shown yourself , itoa can be called with base 2, which stands for binary.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
char str[33];
i = 37; /* Just some number. */
itoa (i, str, 2);
printf("binary: %s\n", str);
return 0;
}
Also, yes, there will be truncation if you are using an integer type other than int, since itoa () only accepts "int" as a value. the longest long on your compiler is probably 64 bits, while an int is probably 32 bits, so the compiler would truncate the 64 bit value to a 32 bit value before converting.
source to share
Your wording is a little confusing, usually if you specify "decimal" I would denote it as "a number represented as a string of decimal digits", while you seem to mean "an integer".
and with "binary", I would call it: "a number represented as bytes - as directly used by the processor."
the best way to formulate your object would be: convert a 64 bit integer to a string of binary digits.
some systems have the _i64toa function.
source to share
Standard way to convert to long long
- strtoull()
and std::strtoull()
for C and C ++ respectively
Example for cppreference
#include <iostream>
#include <cstdlib>
int main()
{
const char* begin = "10 200000000000000000000000000000 30 40";
char *end;
for (unsigned long i = std::strtoul(begin, &end, 10);
begin != end;
i = std::strtoul(begin, &end, 10))
{
begin = end;
if (errno == ERANGE){
std::cout << "range error\n";
errno = 0;
}
std::cout << i << '\n';
}
}
source to share