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
1 Answer 1
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
-
How could it be so easy? Thanks! But help me understand. What's the purpose of the dereference asterisk in this cast?BrainTrance– BrainTrance2020年07月15日 13:38:59 +00:00Commented Jul 15, 2020 at 13:38
-
2Same the other way round:
rf95.send((uint8_t*)udp_buffer, udp_received);
DataFiddler– DataFiddler2020年07月15日 13:39:48 +00:00Commented Jul 15, 2020 at 13:39 -
1a
char
is a character, achar *
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– Michel Keijzers2020年07月15日 13:40:08 +00:00Commented 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.JRobert– JRobert2020年07月15日 15:47:18 +00:00Commented Jul 15, 2020 at 15:47
-
lang-cpp