我可以使用itoa()
将long long int转换为二进制字符串吗? 我看到了使用itoa
将int转换为二进制的各种示例。如果我使用long long int,是否存在溢出或可能丧失精度的风险。在C/C++中使用itoa将整数转换为二进制字符串
编辑 - 谢谢你们所有人的回复。我达到了我想要做的。 itoa()没有足够的用处,因为它不支持long long int.Moreover我不能在gcc中使用itoa(),因为它不是标准的库函数。
我可以使用itoa()
将long long int转换为二进制字符串吗? 我看到了使用itoa
将int转换为二进制的各种示例。如果我使用long long int,是否存在溢出或可能丧失精度的风险。在C/C++中使用itoa将整数转换为二进制字符串
编辑 - 谢谢你们所有人的回复。我达到了我想要做的。 itoa()没有足够的用处,因为它不支持long long int.Moreover我不能在gcc中使用itoa(),因为它不是标准的库函数。
要将整数转换为仅包含二进制数字的字符串,您可以通过使用一位掩码检查整数中的每个位并将其附加到字符串来完成。
事情是这样的:
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;
}
编辑:
这样做可能会使用模板来完成的更一般的方式:
#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;
}
提示:这两种static_assert
和std::is_integral
是是C++ 11的一部分,但在Visual C++ 2010和GCC中至少支持4.4.5。
当然,对于很小的数字,会有很多前导零...... – 2012-03-14 09:38:40
可以很容易地避免前导零。只需跳过0,直到看到前1(从左到右扫描)。 – phoxis 2012-03-14 09:46:16
而且它不使用itoa() – 2012-03-14 09:47:46
是的,你可以。与showed yourself一样,可以用基数2调用itoa,这意味着二进制。
#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;
}
另外,是的,将有截断如果使用非INT的整数类型,因为itoa()只需要简单的“INT”作为值。 long long在编译器中可能是64位,而int可能是32位,所以编译器会在转换之前将64位值截断为32位值。
你的措辞有点令人困惑, 通常如果你说'十进制'我会采取这个意思:'一个数字表示为一串十进制数字',而你似乎意味着'整数'。
和'二进制'我会采取这种意思:'一个数字表示为字节 - 可直接使用的CPU'。
更好的方法来表达你的主题将是:将64位整数转换为二进制数字串。
某些系统具有_i64toa函数。
的标准方法转化成long long
是strtoull()
和std::strtoull()
为C和C++分别
上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';
}
}
AFAIK,'itoa'的整数转换为字符串.....莫非你提供了你提到的那些例子的一些示例代码? – 2012-03-14 09:23:03
'itoa'不是标准功能。另外,你的意思是你想要一个整数转换为只有二进制数字的字符串? – 2012-03-14 09:24:54
@ SayemAhmed-这里是说我可以将int转换为二进制字符串的链接.http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/ – 2012-03-14 09:25:34