Splitting a HEX array

I have a hex array with config information in a buffer
0x32, 0x42, 0x33, 0x00, 0x00, 0x88, 0x88, 0x06, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x66, 0x01, 0xFF

The first 8 is the euID
The second 8 is the appID
The next 16 is the appKey
and the last one is the sysCom (essentiall how to apply the settings)

Is this a reasonable way to handle this or would I be better and would it be faster to use somethign like memcpy()?

      for(uint8_t i=0;i<mcpsIndication->BufferSize;i++) {

        if(i>=0||i<=7){

          NewdevEui[i]=mcpsIndication->Buffer[i];

        }else if(i>=8||i<=15){

          NewappEui[i]=mcpsIndication->Buffer[i];

        }else if(i>=16||i<=31){

          NewappKey[i]=mcpsIndication->Buffer[i];

        }else if(i==32){

          sysCom[i]=mcpsIndication->Buffer[i];

        }
      }
      memcpy(NewdevEui ,mcpsIndication->Buffer, 8);
      memcpy(NewappEui ,mcpsIndication->Buffer+8, 8);
      memcpy(NewappKey ,mcpsIndication->Buffer+16, 16);
      memcpy(sysCom ,mcpsIndication->Buffer+32, 1);

I end up with the data in the array’s
New Eui: 0x32, 0x42, 0x33, 0x00, 0x00, 0x88, 0x88, 0x06
New appEui: 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
New appKey: 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x66, 0x01
sysCom: 0xFF

You probably intended && instead of || .

It is difficult to determine if your code is correct because you only provided an uncompileable snippet and snippets can be misleading. For example, it is impossible to see how variables were declared.

In any case, depending upon what you really want to accomplish, it may be possible to do this with unions or pointers... and variables declared as arrays are actually pointers.

Why make a large array and copy pieces of it into various smaller arrays? What is happening here?

something like this maybe

uint8_t Buffer[33] = { 0x32, 0x42, 0x33, 0x00, 0x00, 0x88, 0x88, 0x06, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x66, 0x01, 0xFF };

struct Blah
{
 uint8_t eui[8];
 uint8_t app_eui[8];
 uint8_t app_key[16];
 uint8_t sys_com;
};

then u can use it like so

Blah bleh{};

 memcpy_s(&bleh, sizeof Blah, Buffer, sizeof Buffer);

 for (auto element : bleh.eui)
 printf("0x%X ", element);

idealy just read the buffer into the struct initially but without how you are using and obtaining the data it is hard to say

Ahh yes, AND instead of OR. Thanks.

Sadly the code is getting a little large to post in its entirety and mcpsIndication->Buffer is comming from a library so posting it would require also posting the library.

A quick and dirty rundown of the working part where I decided to use memcpy.

uint8_t NewdevEui[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
uint8_t NewappEui[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
uint8_t NewappKey[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
uint8_t sysCom[] = {0x00};

uint8_t tmpData[] = {0x32, 0x42, 0x33, 0x00, 0x00, 0x88, 0x88, 0x09, 0xB1, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0xA8, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x66, 0xC1,0xff};

void setup() {
 Serial.begin(115200);
 //check eeprom, load and use settings if the version number is above 0x00 else use defualts

 //Show the existing settings 
 Serial.println("Original Settings");
 Serial.print("New Eui: ");
 dumpByteArray(NewdevEui, (sizeof(NewdevEui)/sizeof(NewdevEui[0])));
 Serial.print("New appEui: ");
 dumpByteArray(NewappEui, (sizeof(NewappEui)/sizeof(NewappEui[0])));
 Serial.print("New appKey: ");
 dumpByteArray(NewappKey, (sizeof(NewappKey)/sizeof(NewappKey[0])));
 Serial.print("command: ");
 dumpByteArray(sysCom, (sizeof(sysCom)/sizeof(sysCom[0])));

 memcpy(NewdevEui ,tmpData, 8);
 memcpy(NewappEui ,tmpData+8, 8);
 memcpy(NewappKey ,tmpData+16, 16);
 memcpy(sysCom ,tmpData+32, 1);
 
 //may be better using switch case here as there will be quite a number of config options
 if(sysCom[0]==0xff){
        Serial.println("activate settings");
         //Show new Settings 
        Serial.println("New Settings");
        Serial.print("New Eui: ");
        dumpByteArray(NewdevEui, (sizeof(NewdevEui)/sizeof(NewdevEui[0])));
        Serial.print("New appEui: ");
        dumpByteArray(NewappEui, (sizeof(NewappEui)/sizeof(NewappEui[0])));
        Serial.print("New appKey: ");
        dumpByteArray(NewappKey, (sizeof(NewappKey)/sizeof(NewappKey[0])));
        Serial.print("command: ");
        dumpByteArray(sysCom, (sizeof(sysCom)/sizeof(sysCom[0])));
 }else if(sysCom[0]==0xfe){
        Serial.println("just store settings");
        //just push the settings to eeprom without restart
 }

}

void loop() {
  // process sensor data

}

void dumpByteArray(const byte * byteArray, const byte arraySize){
for (int i = 0; i < arraySize; i++){
  Serial.print("0x");
  if (byteArray[i] < 0x10)
    Serial.print("0");
  Serial.print(byteArray[i], HEX);
  Serial.print(", ");
}
Serial.println();
}

vaj4088:
Why make a large array and copy pieces of it into various smaller arrays? What is happening here?

the large array is comming in via a radio link. The packet needs to be sent to the unit in base64, then is decided and available in the buffer.

Sending smaller chunks would require multiple transmissions and on this system I need to keep each transmission down to a minimum.

icesythe7:
something like this maybe

uint8_t Buffer[33] = { 0x32, 0x42, 0x33, 0x00, 0x00, 0x88, 0x88, 0x06, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x66, 0x01, 0xFF };

struct Blah
{
uint8_t eui[8];
uint8_t app_eui[8];
uint8_t app_key[16];
uint8_t sys_com;
};



then u can use it like so 


Blah bleh{};

memcpy_s(&bleh, sizeof Blah, Buffer, sizeof Buffer);

for (auto element : bleh.eui)
printf("0x%X ", element);




idealy just read the buffer into the struct initially but without how you are using and obtaining the data it is hard to say

I had thought about using struct for this however it did not play well with the library supplied by the manufacturer of the radio. I did contemplate re-writing parts of it but time constraints for getting this thing up and running are not on my side.

Don't need mem copy.

Remember pointers?

// icesythe7 made up this for you..
struct Blah
{
uint8_t eui[8];
uint8_t app_eui[8];
uint8_t app_key[16];
uint8_t sys_com;
};

// Lets make a pointer for this kind of struct..
Blah* blaPtr;

// Here's yout biffer with everything in it from the radio right?
uint8_t Buffer[33] = { ... };

// The variable blaPtr gets the address of the first item of the untyped byte buffer. this will be allowed because we're telling it to pretend its actually a Bla pointer anyway.
blaPtr = (Blah*)(&Buffer[0]);

//Now here's your goodies..

blaPtr->eui;
blaPtr->app_eui;
blaPtr->app_key;
blaPtr->sys_com;

Pointing a Blah pointer at your buffer is like, err.. Laying a transparency over it. Looking through that, you can access your bits.

Works both ways too! You can load up a complicated buffer from a struct just as easy. Good for boxing up stuff to ship off.

-jim lee

jimLee:
Don't need mem copy.

Remember pointers?

Pointing a Blah pointer at your buffer is like, err.. Laying a transparency over it. Looking through that, you can access your bits.

Works both ways too! You can load up a complicated buffer from a struct just as easy. Good for boxing up stuff to ship off.

-jim lee

Oh wow, that just made life way too easy. I thought you had to tell it where you were starting to read the data from and how long the data was that you were reading.

Thanks Jim, will give that a try.

vangalvin:
the large array is comming in via a radio link. The packet needs to be sent to the unit in base64, then is decided and available in the buffer.

Strange radio link that can only handle ASCII encoded data, but okay.

Edit: PS there’s no such thing as a “hex” array. It’s just an array.

pcbbc:
Strange radio link that can only handle ASCII encoded data, but okay.

Edit: PS there’s no such thing as a “hex” array. It’s just an array.

Yes, its just an array. Sorry for any confusion with calling it a "Hex" array or Array of Hex Values.

Totally agree, its a pain that the data has to be base64 encoded, The LoRa gateway has its front end written and the way data is posted to the remote device works so im a bit stuck with it at the moment. Options were either sending Base64 or Json and using Json was going to blow away the 51 byte limit once I add the channel mask and the rest of the config data. :frowning: