c++将2字节的十六进制转换为整数
c++ convert 2 bytes of hex into integer
我正在开发一个小的嵌入式应用程序,它发送和响应这样的数据块{80,09,1D,00,00}
(所有值都是十六进制格式),存放在vector<int>
中。我一直在寻找一种方法,将这些字节转换为类型integer
(c++)。我一直在寻找,但我不能得到正确的答案,我有这个代码来测试结果:
#include <iostream>
#include<stdio.h>
#include <vector>
using namespace std;
int main()
{
int x = 0x2008;//Expected Number Result from the process
vector<int> aCmdResult;
aCmdResult.push_back(0x20);
aCmdResult.push_back(0x08);
int aresult=0;
aresult= (aCmdResult.at(0)&0xFF) | ( (aCmdResult.at(1)>>8)&0xFF) ;
cout<<std::hex<<"conversion Result: "<<aresult<<endl;
/* two bytes of hex = 4 characters, plus NULL terminator */
x=0x0014;//Expected Number Result from the process
aresult=0;
aCmdResult.clear();
aCmdResult.push_back(0x00);
aCmdResult.push_back(0x14);
aresult= (aCmdResult.at(0)&0xFF) | ( (aCmdResult.at(1)>>8)&0xFF) ;
cout<<std::hex<<"conversion Result: "<<aresult<<endl;
return 0;
}
第一个输出的结果是:0第二个是:20但这些都不是正确的价值观!提前感谢
最后:
#include <iostream>
#include<stdio.h>
#include <vector>
using namespace std;
int main()
{
int x = 0x2008;//Expected Number Result from the process
vector<int> aCmdResult;
aCmdResult.push_back(0x20);
aCmdResult.push_back(0x08);
int aresult=0;
aresult= ((aCmdResult.at(0)&0xFF)<<8) | ( (aCmdResult.at(1))&0xFF) ;
cout<<std::hex<<"conversion Result: "<<aresult<<endl;
/* two bytes of hex = 4 characters, plus NULL terminator */
x=0x0014;//Expected Number Result from the process
aresult=0;
aCmdResult.clear();
aCmdResult.push_back(0x00);
aCmdResult.push_back(0x14);
aresult= (aCmdResult.at(0)<<8&0xFF) | ( (aCmdResult.at(1))&0xFF) ;
cout<<std::hex<<"conversion Result: "<<aresult<<endl;
return 0;
}
我想做的是来回转换从数组到整数,并返回到整数(我认为这是从右边的两个字节使用异或??)
int aresult = std::accumulate( aCmdResult.begin(), std::next( aCmdResult.begin(), sizeof( int ) ), 0,
[] ( int acc, int x ) { return ( acc << 8 | x ); } );
如果只需要组合两个字节和sizeof(int) != 2,则替换为
std::next( aCmdResult.begin(), sizeof( int ) )
std::next( aCmdResult.begin(), 2 )
下面是使用
算法的示例#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <iterator>
int main()
{
std::vector<int> v;
v.reserve( sizeof( int ) );
int z = 0x12345678;
std::generate_n( std::back_inserter( v ), sizeof( int ),
[&]() -> int { int x = z & 0xFF; z = ( unsigned ) z >> 8; return x; } );
std::reverse( v.begin(), v.end() );
int x = std::accumulate( v.begin(), v.end(), 0,
[] ( int acc, int x ) { return ( acc << 8 | x ); } );
std::cout << std::hex << x << std::endl;
}
相关文章:
- 努力将整数转换为链表。不知道我在这里做错了什么
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 如何在C++中将整数转换为其数字数组
- 尝试将字符串/字符转换为整数会产生意外结果
- 如何使用C++将字符串中的字符转换为整数变量
- 如何将整数字符串转换为整数的二维向量?
- 如果整数与指针大小相同,则重新解释将整数转换为指针双射是否具有双射作用?
- 使用 "stringstream " 将字符串转换为整数
- 将字符串转换为浮点数或整数,而无需使用内置函数(如 atoi 或 atof)
- 如何检测 std::vector::emplace_back 上的隐式转换损失整数精度
- 如何防止双精度值到整数的隐式转换
- 为什么此指针值不能转换为整数的规则是什么?
- 如何解决隐式转换丢失整数精度:'size_t'(又名"无符号长")到'int'警告?
- 在 C++ 中将整数数组转换为位集表示形式的最佳方法?
- 为什么在将 void 指针转换为整数指针时出现分段错误
- 将字符串(可以是十进制字符串或十六进制字符串)转换为整数C++
- 整数类型应该显式转换(例如"int"到"无符号")还是只会增加混乱?
- 如何使用 STL 算法将整数向量转换为字符串向量?
- 无法将"整数 (*)[m]"转换为"整数 (*)[100000]
- 静态强制转换允许转换对象指针,但不允许转换整数