GeeRodreezy
GeeRodreezy

Reputation: 1

Byte Swap with an array?

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.

I am reading a list that resembles something like: S315FFF200207F7FFFFF42A000000000001B000000647C S315FFF2003041A00000FF7FFFFF0000001B00000064ED S315FFF2004042480000FF7FFFFF0000001E000000464F

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.

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.

EDIT: 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...

EDIT2: 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);

I think I may need to convert my STOI to a short in some way..

EDIT3: 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;

Upvotes: 0

Views: 4167

Answers (2)

Kirkova
Kirkova

Reputation: 347

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)

EDIT: 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;
}

it only matters to change the loBit and hiBit to be unsigned since those are the temporary values we're using.

Upvotes: 1

Dark Squirtings
Dark Squirtings

Reputation: 198

If you're asking what I think you're asking-

First, you need to make sure you know what size your integers are. 32 bits is nice and standard, but check and make sure.

Second, cast your integer array as a char array. Now you can access and manipulate the array one byte at a time.

Third- just reverse the order of every four bytes (after your first 12 char offset). Swap the first and fourth and the second and third.

Upvotes: 0

Related Questions