I have a char array which represents a GUID as bytes (not as chars) but I have to reverse half of the array. That happened because I used sscanf to convert a GUID string into char array (which represents bytes) using:
sscanf(strguid,"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
,&arr[0],&arr[1],
,&arr[2],&arr[3],....,&arr[15]);
The array I have is for example:
2EC5D8AA85E74B5E872462155EAA9D51
and I have to reverse it so it will give the right GUID:
AAD8C52EE7855E4B872462155EAA9D51
What I tried it the following:
unsigned int temp;
memcpy(&temp,&arr[0],sizeof(char));
memcpy(&arr[0],&arr[3],sizeof(char));
memcpy(,&arr[3],&temp,sizeof(char));
And so on. (The second with the third, the fifth with the sixth and the seventh with the eighth)
Is there an easier way to do that?
If i understand you problem correctly you need change endianness of 3 first members of GUID
struct
typedef struct {
unsigned long Data1;
unsigned short Data2;
unsigned short Data3;
byte Data4[ 8 ];
} GUID;
You can try this
std::reverse(guid_, guid_ + 4);
std::reverse(guid_ + 4, guid_ + 6);
std::reverse(guid_ + 6, guid_ + 8);
But i'd prefer changing sscanf format like this
const char *string_ = "AAD8C52E-E785-5E4B-8724-62155EAA9D51";
GUID guid_;
sscanf(string_, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
&guid_.Data1, &guid_.Data2, &guid_.Data3,
&guid_.Data4[0], &guid_.Data4[1], &guid_.Data4[2], &guid_.Data4[3], &guid_.Data4[4], &guid_.Data4[5], &guid_.Data4[6], &guid_.Data4[7]);
Be advised that you need to check input string length to avoid shorter string parsing