# Reverse Data

How can I reverse HEX data Exp {35 45 6D} to {6D 45 35}? Thank you

You could start by posting your code and a fuller description of the problem.

Briefly, copy from source array while incrementing index and store in destn array while decrementing index.

How can I reverse HEX data Exp {35 45 6D} to {6D 45 35}? Thank you

dougp:
Briefly, copy from source array while incrementing index and store in destn array while decrementing index.

``````void setup()
{
Serial.begin(9600);
byte srcArray[] = {0x35, 0x45, 0x6D};
byte dstArray[3];

for(int srcIndex = 2, dstIndex = 0; srcIndex>=0, dstIndex<=2; srcIndex--, dstIndex++)
{
dstArray[dstIndex] = srcArray[srcIndex];
Serial.print(dstArray[dstIndex], HEX);
Serial.print(" ");
}
}

void loop()
{

}
``````

reverse HEX data Exp {35 45 6D} to {6D 45 35}

``````byte temp;
temp = data[2];
data[2] = data[0];
data[0] = temp;
``````

Or something more generic that will work with any size array:

``````for (byte i = 0; i < (arraysize / 2); i++) {
temp = test[i];
test[i] = test[(arraysize - 1) - i];
test[(arraysize - 1) - i] = temp;
}
``````
``````// Example of inverting elements in an array
int test[25];
int temp;
byte arraysize = sizeof(test) / sizeof(test[0]);

void setup() {
Serial.begin(9600);
for (byte i = 0; i < arraysize; i++) { //fill array with sequential numbers
test[i] = i;
}
for (byte i = 0; i < arraysize; i++) { //print array contents
Serial.print("Test[");
Serial.print(i);
Serial.print("]=");
Serial.println(test[i]);
}
Serial.println();
delay(5000);
}

void loop() {
for (byte i = 0; i < (arraysize / 2); i++) {
Serial.print("swapping elements ");
Serial.print(i); Serial.print(" <> ");
Serial.println(arraysize - 1 - i);
temp = test[i];
test[i] = test[(arraysize - 1) - i];
test[(arraysize - 1) - i] = temp;
}
Serial.println("");
for (byte i = 0; i < arraysize; i++) { //print array contents
Serial.print("Test[");
Serial.print(i);
Serial.print("]=");
Serial.println(test[i]);
}
Serial.println();
delay(30000);
}
``````

Five answers to an XY problem?