简体   繁体   English

C ++将2字节的十六进制转换为整数

[英]c++ convert 2 bytes of hex into integer

I am developing a little embed app that sends and responds with a chunk of data like this {80,09,1D,00,00} (all the values are in hexadecimal format) and it is being stored in a vector<int> . 我正在开发一个嵌入式应用程序,它使用诸如{80,09,1D,00,00}这样的数据块来发送和响应(所有值均采用十六进制格式),并将其存储在vector<int> I have been looking for a method that transforms this bytes int a type integer (c++). 我一直在寻找一种将此字节转换为integer类型(c ++)的方法。 I have been looking around but I cannot get the right answer, I have this code to test the results: 我一直在环顾四周,但是我找不到正确的答案,我有这段代码可以测试结果:

#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;
}

The result for the first output is: 0 and in the second is: 20 but these are not the correct values! 第一个输出的结果是:0,第二个输出的结果是:20,但这不是正确的值! Thanks in advance 提前致谢

int aresult = std::accumulate( aCmdResult.begin(), std::next( aCmdResult.begin(), sizeof( int ) ), 0,
                               [] ( int acc, int x ) { return ( acc << 8 | x ); } );

If you need to combine only two bytes and sizeof( int ) != 2 then substitute 如果只需要组合两个字节和sizeof(int)!= 2,则替换

std::next( aCmdResult.begin(), sizeof( int ) )

for 对于

std::next( aCmdResult.begin(), 2 )

Here is an example of using the algorithm 这是使用算法的示例

#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;
}

finally : 最后:

#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;
}

what i tryng to do is to back and forth the convertion from the array to integer , and the back to integer (i think that is using XOR from the two bytes from the rigth side??) 我想做的是从数组到整数的来回转换,再到整数的转换(我认为这是从严格的两个字节开始使用XOR?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM