0

I have a char array variable that has a value. I need to send this value through LoRa. The library I use for the LoRa implementation accepts an array of uint8_t. How can I pass the value of a char[] to a uint8_t[] variable (and the opposite)?

if (Udp.parsePacket()) {
  int udp_received = Udp.available();
  char udp_buffer[udp_received + 1];
  Udp.read(udp_buffer, udp_received);
  udp_buffer[udp_received] = '\0';

  uint8_t udp_to_lora[] = "???";
  rf95.send(udp_to_lora, sizeof(udp_to_lora));
  rf95.waitPacketSent();

}

I need to pass the udp_buffer to uint8_t udp_to_lora[].

0

I don't have a compiler at hand, but you can cast the array as a char and uint8_t are similar in size:

rf95.send((char*) udp_to_lora, sizeof(udp_to_lora));
| improve this answer | |
  • How could it be so easy? Thanks! But help me understand. What's the purpose of the dereference asterisk in this cast? – BrainTrance Jul 15 at 13:38
  • 2
    Same the other way round: rf95.send((uint8_t*)udp_buffer, udp_received); – DataFiddler Jul 15 at 13:39
  • 1
    a char is a character, a char * is a POINTER to a character. However, an array of characters is the same, as it points to the first character of a text. – Michel Keijzers Jul 15 at 13:40
  • "What's the purpose of the dereference asterisk in this cast?" It's in the context. In a type specification (which a cast is) the '*' is part of the type, where it means "pointer to". In a value, it is the dereference operator and means "the value of what [thing] points to", or more literally, "the value contained in the memory location(s) whose address is the value of [thing]". Locations is plural because Arduino memory, being byte addressable, most variables are multi-byte. – JRobert Jul 15 at 15:47
  • dereference is *a – Juraj Jul 15 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.