We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi everyone, I have an array eg:
command[9] = {0x3C , 0x4A , 0x33 , 0x25 , 0xB1 , 0xD3 , 0x59 , 0x34 , 0x2E }
//init outside unsigned char k[4]; long value;
k[0] = command [5]; k[1] = command [6]; k[2] = command [7]; k[3] = command [8]; value= strtol (k, NULL, 4); value= value+3; sprintf ( k, "%Ld\n" , value); buf[4] = k[0]; buf[5] = k[1]; buf[6] = k[2]; buf[7] = k[3]; sendcom ( buf , 15);//send to serial function
Lots of ways to go about this. If your data is actually structured, then why not structure it?
typedef struct { CmdHeader header; // 5 bytes U32 value; } Command; Command command; ... command.value += 3; sendcom ((U8*)&command, sizeof(command));
U8 command[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; #define MyCmdValueOffset 5 ... // find a pointer to value by adding 5 // to the start of the command buffer, // and casting to long, *(long*)(command + MyCmdValueOffset) += 3;
"If your data is actually structured, then why not structure it? ... If you do want to keep the command in a byte array..." You could use a union - then you could have your cake as a struct and eat it in bytes, too!