This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Array and Long manipulations

Hi everyone,

I have an array eg:

command[9] = {0x3C , 0x4A , 0x33 , 0x25 , 0xB1 , 0xD3 , 0x59 , 0x34 , 0x2E }

the last 4 byte is the value (in hex) that I need to manipulate, let say increment by 3.

and after manipulation I need to send back the value together with other data in one array to serial port.

What I had try is:

//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


But it not working.

Can anyone give me help, and honestly I'm lack of C basics

Thanks

Juru

Parents
    1. comand[9] (0xD3) is not an ASCII character
    2. k[], when supplied to strtol() is not big enough to hold the NUL-termintor that you are not even supplying to terminate the "string"
    3. The strtol() call's third parameter (4) specifies base-4 conversion -- probably not what you want

Reply
    1. comand[9] (0xD3) is not an ASCII character
    2. k[], when supplied to strtol() is not big enough to hold the NUL-termintor that you are not even supplying to terminate the "string"
    3. The strtol() call's third parameter (4) specifies base-4 conversion -- probably not what you want

Children
  • Oops, in my first list item, make that command[5].

  • 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));
    

    Just because the sendcom routine sends a series of bytes doesn't mean the entire rest of your program needs to treat the data that way.

    Presumably that "CmdHeader" has some further structure, but your post doesn't describe the data.

    If you do want to keep the command in a byte array, then you can also do some pointer surgery:

    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;
    

    Note that this is exactly what the version with the struct does, except that the compiler does the work for you so that you do not have to manually maintain the value offset.

    sprintf() operates on strings. It looks like you have an actual binary integer in that array.

  • "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!