Working with HEX numbers

I have a function that accepts 2 arguments/parameters (command and data) which I then put into an 8-byte buffer and write to software serial.

static int8_t Send_buf[8] = {0};

void sendCommand(int8_t command, int16_t dat)
{
  Send_buf[0] = 0x7e; // start byte
  Send_buf[1] = 0xff; // version number
  Send_buf[2] = 0x06; // number of bytes of the command without starting and ending bytes
  Send_buf[3] = command; //
  Send_buf[4] = 0x00; // 0x00 for no feedback
  Send_buf[5] = (int8_t)(dat >> 8);
  Send_buf[6] = (int8_t)(dat);
  Send_buf[7] = 0xef; // end byte
  for(uint8_t i=0; i<8; i++)
  {
    mySerial.write(Send_buf[i]);
  }
}

The second argument is 2-bytes: 1st byte defines folder number, 2nd byte defines file number.

I have folder and file index position variables: ex. byte ix_folder = 1; byte ix_file = 1;

How do I join these two numbers into one hex number: 0x0101

sendCommand(0x0F, 0x0101);

I managed to work it out:

byte p1 = 0x01;
byte p2 = ix_pos;
int16_t val = p1 * 256 + p2; // OR int16_t val = (p1 << 8) + p2;
sendCommand(0x0F, val);

I managed to work it out:

How does that left shift by smiley face work?

Question 1, why is the buffer global and not part of the function?

Question 2, why use a buffer? You can just send it as you go...

septillion:
Question 1, why is the buffer global and not part of the function?

Question 2, why use a buffer? You can just send it as you go...

I am using a buffer because I was doing other things with it, but yes it can just be a series of mySerial.write() commands.

Initially I was referring to the buffer from other functions - that's not the case now, so it's no longer static or global and is declared inside the sendCommand function.

wow I can't make 2 posts without a 10 minute period between :frowning:

my smiley face is an 8

my (pruned down) code is:

int16_t val = (0x01 << 8) + ix_pos;
sendCommand(0x0F, val);

How about

  sendCommand(0x0F, 1, ix_pos);
  
void sendCommand(int8_t command, int8_t highB, int8_t lowB)
{
int8_t Send_buf[8];
  Send_buf[0] = 0x7e; // start byte
  Send_buf[1] = 0xff; // version number
  Send_buf[2] = 0x06; // number of bytes of the command without starting and ending bytes
  Send_buf[3] = command; //
  Send_buf[4] = 0x00; // 0x00 for no feedback
  Send_buf[5] = highB;
  Send_buf[6] = lowB;
  Send_buf[7] = 0xef; // end byte
  for(uint8_t i=0; i<8; i++)
  {
    mySerial.write(Send_buf[i]);
  }
}

so you dont have to combine and split at all??

b4ip:
wow I can't make 2 posts without a 10 minute period between :frowning:

That's why there is an edit...

b4ip:
my smiley face is an 8

We know, just a lesson to use code tags :wink:

Re Whandall's suggestion:

How about:

  mySerial.write(Send_buf, sizeof(Send_buf));

Of course that's doing internally the same loop as you wrote yourself, but why not using code that's already available anyway?

or shorter

sendCommand(0x0F, 1, ix_pos);

void sendCommand(int8_t command, int8_t highB, int8_t lowB) {
  static int8_t Send_buf[8] = {
    // start, version, len, hData, lData, end
    0x7E, 0xFF, 0x06, 0, 0x00, 0, 0, 0xEF
  };
  Send_buf[3] = command;
  Send_buf[5] = highB;
  Send_buf[6] = lowB;
  mySerial.write(Send_buf, 8);
}

Look at the recent post:

Sending and Receiving an Integer Through Serial

It shows a clever way to use the union operation to combine
arrays to automatically go from bytes to integers by just
putting them into an array.
No shift or multiply operation. The array you use to hold the values
can do all the work for you.
Dwight

thanks septillion, I had a feeling you hard-core C programmers were just teasing me.

I learned C when I was 12 y.o and then hardly did any programming in it until about 6 months ago, but I did manage to find some of my old notes, and of course this great forum.

thank you to all for your help, feedback and suggestions.