C++ 32位转换为C+中的unit64+;
我想把一个32位的值放在unsigned int中。我想让这个程序在32位和64位int的平台上运行,同时使用little和big-endian。这是我的密码C++ 32位转换为C+中的unit64+;,c++,memcpy,C++,Memcpy,我想把一个32位的值放在unsigned int中。我想让这个程序在32位和64位int的平台上运行,同时使用little和big-endian。这是我的密码 void uCHarToInt(unsigned char* input, unsigned int* oputput) { memcpy(oputput, reinterpret_cast<unsigned int*>(&input), 4); if (sizeof(unsigne
void uCHarToInt(unsigned char* input, unsigned int* oputput)
{
memcpy(oputput, reinterpret_cast<unsigned int*>(&input), 4);
if (sizeof(unsigned int) == 8)
{
*oputput >>= 32;
}
}
void uchartPoint(无符号字符*输入,无符号整数*输出)
{
memcpy(输出、重新解释和输入),4;
if(sizeof(unsigned int)==8)
{
*oputput>>=32;
}
}
我认为这将适用于big-endnian以及32位和64位整数类型,但我不确定little-endian以及memcpy在不同平台上的行为。难道没有更好的解决办法吗
难道没有更好的解决办法吗
确实有
从这个问题上看,不清楚给定的字节序列是小端还是大端(我假设它们是作为通信协议的一部分到达的?)
以下函数将正确执行转换,而不管接收主机的字长或尾数:
#include <iostream>
#include <iomanip>
#include <iterator>
#include <algorithm>
template<class Iter>
unsigned int accumulate_value(Iter first, Iter last)
{
unsigned int result = 0;
for( ; first != last ; ++first)
{
result <<= 8;
result |= *first;
}
return result;
}
unsigned int from_big_endian_stream(const unsigned char* bytes, size_t size = 4)
{
return accumulate_value(bytes, bytes + size);
}
unsigned int from_little_endian_stream(const unsigned char* bytes,
size_t size = 4)
{
return accumulate_value(std::make_reverse_iterator(bytes+4),
std::make_reverse_iterator(bytes));
}
int main()
{
unsigned char little_endian_data[] = { 3, 0, 2, 0 };
unsigned char big_endian_data[] = { 0, 2, 0, 3 };
std::cout << std::hex << from_little_endian_stream(little_endian_data)
<< std::endl;
std::cout << std::hex << from_big_endian_stream(big_endian_data)
<< std::endl;
}
难道没有更好的解决办法吗
确实有
从这个问题上看,不清楚给定的字节序列是小端还是大端(我假设它们是作为通信协议的一部分到达的?)
以下函数将正确执行转换,而不管接收主机的字长或尾数:
#include <iostream>
#include <iomanip>
#include <iterator>
#include <algorithm>
template<class Iter>
unsigned int accumulate_value(Iter first, Iter last)
{
unsigned int result = 0;
for( ; first != last ; ++first)
{
result <<= 8;
result |= *first;
}
return result;
}
unsigned int from_big_endian_stream(const unsigned char* bytes, size_t size = 4)
{
return accumulate_value(bytes, bytes + size);
}
unsigned int from_little_endian_stream(const unsigned char* bytes,
size_t size = 4)
{
return accumulate_value(std::make_reverse_iterator(bytes+4),
std::make_reverse_iterator(bytes));
}
int main()
{
unsigned char little_endian_data[] = { 3, 0, 2, 0 };
unsigned char big_endian_data[] = { 0, 2, 0, 3 };
std::cout << std::hex << from_little_endian_stream(little_endian_data)
<< std::endl;
std::cout << std::hex << from_big_endian_stream(big_endian_data)
<< std::endl;
}
你为什么认为你需要在这里重新解释演员阵容?你为什么不直接使用
uint32\u t
,它保证有你想要的准确大小?我重复使用了一些旧代码,我建议使用这个cast。。我有返回无符号整数的函数,但似乎最好改变它们。。。所以这在所有平台上都有效是吗?void uchartPoint(unsigned char*input,uint32_t*oputput){memcpy(oputput,input,4)}忘记代码吧。描述你想要实现的目标,不要提及你是如何尝试实现的。因为很明显,这段代码至少可以说很奇怪。@gnasher729我想把输入的下一个32位的序列存储到变量中,这样我就可以把它当作数字来使用,并且我希望这段代码尽可能健壮。。所以我在不同的地方得到相同的数字platforms@RainMaker但是一些32位的序列可以表示许多数字。您希望它代表什么数字?位的顺序是什么?为什么要使用memcpy
?为什么不把单个字节移到适当的位置呢?为什么您认为您需要在这里重新解释强制转换?你为什么不直接使用uint32\u t
,它保证有你想要的准确大小?我重复使用了一些旧代码,我建议使用这个cast。。我有返回无符号整数的函数,但似乎最好改变它们。。。所以这在所有平台上都有效是吗?void uchartPoint(unsigned char*input,uint32_t*oputput){memcpy(oputput,input,4)}忘记代码吧。描述你想要实现的目标,不要提及你是如何尝试实现的。因为很明显,这段代码至少可以说很奇怪。@gnasher729我想把输入的下一个32位的序列存储到变量中,这样我就可以把它当作数字来使用,并且我希望这段代码尽可能健壮。。所以我在不同的地方得到相同的数字platforms@RainMaker但是一些32位的序列可以表示许多数字。您希望它代表什么数字?位的顺序是什么?为什么要使用memcpy
?为什么不把单个字节移到适当的位置呢?