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[].

Michel Keijzers
13k7 gold badges41 silver badges58 bronze badges
asked Jul 15, 2020 at 13:21

1 Answer 1

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));
answered Jul 15, 2020 at 13:23
5
  • How could it be so easy? Thanks! But help me understand. What's the purpose of the dereference asterisk in this cast? Commented Jul 15, 2020 at 13:38
  • 2
    Same the other way round: rf95.send((uint8_t*)udp_buffer, udp_received); Commented Jul 15, 2020 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. Commented Jul 15, 2020 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. Commented Jul 15, 2020 at 15:47
  • dereference is *a Commented Jul 15, 2020 at 15:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.