简体   繁体   中英

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> . I have been looking for a method that transforms this bytes int a type 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! 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

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??)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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