字节交换与数组? [英] Byte Swap with an array?
问题描述
首先,请原谅我极其业余的编码知识.我在一家公司实习,并被指派在C ++中创建一个代码,该代码交换字节以获取正确的校验和值.
First of all, forgive my extremely amateur coding knowledge. I am intern at a company and have been assigned to create a code in C++ that swaps bytes in order to get the correct checksum value.
我正在阅读类似于以下内容的列表:S315FFF200207F7FFFFF42A000000000001B000000647CS315FFF2003041A00000FF7FFFFF0000001B00000064EDS315FFF2004042480000FF7FFFFF0000001E000000464F
I am reading a list that resembles something like: S315FFF200207F7FFFFF42A000000000001B000000647C S315FFF2003041A00000FF7FFFFF0000001B00000064ED S315FFF2004042480000FF7FFFFF0000001E000000464F
我已经使程序将该字符串转换为十六进制,然后将其转换为int,以便可以正确读取它.我没有读每行的前12个字符或后2个字符.
I have made the program convert this string to hex and then int so that it can be read correctly. I am not reading the first 12 chars or last 2 chars of each line.
我的问题是如何使转换后的int进行字节交换(小尾数到大尾数),以便计算机可以读取?再次抱歉,这是一个糟糕的解释.
My question is how do I make the converted int do a byte swap (little endian to big endian) so that it is readable to the computer? Again I'm sorry if this is a terrible explanation.
我基本上需要占用每个字节(4个字母)并将其翻转.即:64C7翻转到C764等,等等.我该怎么做并将其放入新的阵列中?每行现在是一个字符串...
I need to essentially take each byte (4 letters) and flip them. i.e: 64C7 flipped to C764, etc etc etc. How would I do this and put it into a new array? Each line is a string right now...
到目前为止,这是我代码的一部分...
This is part of my code as of now...
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转换为简称.
I think I may need to convert my STOI to a short in some way..
使用下面的答案代码,我得到以下信息...十六进制:8D->以141(十进制)的形式存储到内存(myMem =无符号短整数)->字节交换时:-29440这是怎么了?
Using the answer code below I get the following... HEX: 8D --> stored to memory (myMem = unsigned short) as 141 (decimal) -->when byte swapped: -29440 Whats wrong here??
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:
You can always do it bitwise too. (Assuming 16-bit word) For example, if you're byte swapping an 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)
我确切地尝试使用您的代码
My exact attempt at using your code
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更改为无符号,因为它们是我们正在使用的临时值.
it only matters to change the loBit and hiBit to be unsigned since those are the temporary values we're using.
这篇关于字节交换与数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!