与数组进行字节交换

Byte Swap with an array?

本文关键字:字节 交换 数组      更新时间:2023-10-16

首先,请原谅我非常业余的编码知识。我是一家公司的实习生,被分配在c++中创建一个代码,交换字节以获得正确的校验和值。

我正在阅读一个类似于以下内容的列表:S315FFF200207F7FFFFF42A000000000001B000000647CS315FFF2003041A00000FF7FFFFF0000001B00000064EDS315FFF2004042480000FF7FFFFF0000001E000000464F

我已经让程序将这个字符串转换为十六进制,然后再转换为整型,这样它就可以被正确读取。我没有读取每行的前12个字符或最后2个字符。

我的问题是我如何使转换成int做字节交换(小端到大端),使它是可读的计算机?如果这是一个糟糕的解释,我再次表示抱歉。

编辑:我基本上需要每个字节(4个字母)并翻转它们。比如:64C7变成了C764,等等等等。我怎么把它放到一个新的数组中呢?现在每一行都是一个字符串…

EDIT2:这是我的代码的一部分,因为现在…

    int j = 12;
                    for (i = 0; i < hexLength2 - 5; i++){
                        string convert1 = ODL.substr(j, 4);
                        short input_int = stoi(convert1);
                        short lowBit = 0x00FF & input_int;
                        short hiBit = 0xFF00 & input_int;
                        short byteSwap = (lowBit << 8) | (hiBit >> 8);

我想我可能需要以某种方式将我的STOI转换为short .

EDIT3:使用下面的答案代码,我得到以下…HEX: 8D ->存储到内存(myMem = unsigned short)为141(十进制)->当字节交换时:-29440怎么了?

            for (i = 0; i < hexLength2 - 5; i++){
                        string convert1 = ODL.substr(j, 2);
                        stringstream str1;
                        str1 << convert1;
                        str1 >> hex >> myMem[k];
                        short input_int = myMem[k];                     //byte swap 
                        short lowBit = 0x00FF & input_int;              
                        short hiBit = 0xFF00 & input_int;               
                        short byteSwap = (lowBit << 8) | (hiBit >> 8);  
                        cout << input_int << endl << "BYTE SWAP: " <<byteSwap <<"Byte Swap End" << endl;    
                        k++;
                        j += 2;

您也可以按位操作。(假设是16位字)例如,如果您要交换int:

short input_int = 123; // each of the ints that you have
short input_lower_half = 0x00FF & input_int;
short input_upper_half = 0xFF00 & input_int;
// size of short is 16-bits, so shift the bits halfway in each direction that they were originally
 short byte_swapped_int = (input_lower_half << 8) | (input_upper_half >> 8)

编辑:我的确切尝试使用你的代码

unsigned short myMem[20];
int k = 0;
string ODL = "S315FFF2000000008DC7000036B400003030303030319A";
int j = 12;
for(int i = 0; i < (ODL.length()-12)/4; i++) { // not exactly sure what your loop condition was
    string convert1 = ODL.substr(j, 4);
    cout << "substring is: " << convert1 << endl;
    stringstream str1;
    str1 << convert1;
    str1 >> hex >> myMem[k];
    short input_int = myMem[k];                     //byte swap
    unsigned short lowBit = 0x00FF & input_int; // changed this to unsigned
    unsigned short hiBit = 0xFF00 & input_int; // changed this to unsigned
    short byteSwap = (lowBit << 8) | (hiBit >> 8);
    cout << hex << input_int << " BYTE SWAPed as: " << byteSwap <<", Byte Swap End" << endl;
    k++;
    j += 4;
}

只需要将loBit和hiBit更改为unsigned,因为它们是我们使用的临时值

如果你问的和我想的一样-

首先,您需要确保知道整型的大小。32位是很好的标准,但是要检查并确保。

第二,将整型数组转换为char数组。现在可以一次一个字节地访问和操作数组了。

第三-只需反转每四个字节的顺序(在您的前12个字符偏移量之后)。交换第一个和第四个,第二个和第三个。