Decimal Int to 4 byte hex string

I'm communicating with a closed loop stepper motor via serial. Not problem at all sending standard commands. Now I would like to send a command that has a variable component.

The message I send typically takes the following format:

const byte MotorMoverewind[] = {0xe0, 0xfd, 0x82, 0x00, 0x00, 0xc3, 0x50, 0x72};

I'll break the message components down:

0xe0 - UART identifier
0xfd - Command ID
0x82 - direction and speed byte
0x00, 0x00, 0xc3, 0x50 - Number of steps
0x72 - checksum byte

I've bolded the four bytes i'm interested in. in the above example, the number of steps is 50000 (dec)

So, for an example, if my varying number of steps is 57648671 (dec), i'd like to create the string of bytes thus:

const byte MotorMoverewind[] = {0xe0, 0xfd, 0x82, 0x03, 0x6f, 0xa6, 0x1f, 0x96}

The number of steps will vary each time, so I can't just code a set of known values. Some guidance on the best way to achieve this would be appreciated.

I may have a follow-up question on how to resolve the checksum, but hopefully i'll be able to resolve that myself when I get to it. One step at a time.

It is not a string, it is an array of bytes.
It presented in HEX just as representation for human eyes. You do not need to convert your decimal to hex, just copy four bytes of unsigned long integer to positions 3-7 in MotorMoverewind[] array. It can be done with memcpy() function , for example.
(To doing that your array must not be const, of course)

Thank you @b707.
Conceptually, something like this (haven't had a chance to code it - will check tomorrow):

uint32_t d15 = 57648671;
byte MotorMoverewind[] = {0xe0, 0xfd, 0x82, 0x00, 0x00, 0x00, 0x00, 0x72};
for (int n = 3; n < 7; n++) {
  MotorMoverewind[n] = (d15 >> (n-3)*8) & 0xFF;
}
1 Like

looks great

Remember that the Arduino UNO/Nano is LittleEndian so the value 57648671 is in memory as 1F A6 6F 03.

Thanks John. I tried my conceptual version above and it also ran BigEndian. Adjusted my full test code to suit working backwards:

uint32_t d15 = 57648671;
byte MotorMoverewind[] = {0xe0, 0xfd, 0x82, 0x00, 0x00, 0x00, 0x00, 0x72};

void setup() {

  Serial.begin(9600);

  for (int n = 6; n > 2; n--) {
    MotorMoverewind[n] = (d15 >> (6-n)*8) & 0xFF;
  }

  for (int n = 0; n < 8; n++) {
    Serial.print("0x");
    Serial.print(MotorMoverewind[n],HEX);
    Serial.print(", ");
  }
  
  Serial.println();
}

Now I'll move onto resolving the checksum on that last byte.

The checksums (0x72 and 0x96 here) seem to be the 8-bit sum of all the previous bytes. Pretty easy.

Yes. Just enjoying the day at the beach and water park with the kids. Will play with it later! :smiley:

Yep, pretty easy. Here's my code for posterity. I'll now roll it into my main code. Thank you both for your guidance.

uint32_t d15 = 3001; //Data value I want to insert
byte MotorMoverewind[] = {0xe0, 0xfd, 0x82, 0x00, 0x00, 0x00, 0x00, 0x00}; //Standard message component
int tCHK = 0; //Checksum variable

void setup() {

  Serial.begin(9600);

  //Insert Required Data into Array
  for (int n = 6; n > 2; n--) {
    MotorMoverewind[n] = (d15 >> (6-n)*8) & 0xFF;
  }

  //Generate checksum and insert that at the end of the array
  for (int n = 0; n < 7; n++) {
    tCHK = tCHK + MotorMoverewind[n];
  }
  tCHK = tCHK & 0xFF;
  MotorMoverewind[7] = tCHK;

  //Print Array for checking
  for (int n = 0; n < 8; n++) {
    Serial.print("0x");
    Serial.print(MotorMoverewind[n],HEX);
    Serial.print(", ");
  }
  Serial.println();
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.