Example for converting char to byte using byte() conversion function

Dear all,

I am trying to convert a char array to a byte array. I need to use this byte array as pipe addresses for nRF24L01 module.

I referred to Arduino's documentation on byte() function to convert any data type to byte. Although it looks promising, it is not supported with an example.

Has anyone used this function before? I appreciate your help.

Thanks,
Raj

Welcome to the forum

Your topic was MOVED to its current forum category which is more appropriate than the original as it is not an Introductory Tutorial

byte mybyte = byte(whatever);

You don't need convert it because the byte array and char array are compatible.
Please show the code where you encountered with problem

Thanks for your response.

I tried the following but it didn't work, do you spot any errors?

// Converting char array to byte array
void setup(){
  Serial.begin(9600);
  char whatever []= "NEW01"; // new pipe address 
  byte mybyte [6]= byte(whatever); // converting char to byte

}
void loop(){

}

Please note that I am fairly new to Arduino. I appreciate your continued support.

char whatever []= "NEW01"; // new pipe address 
byte mybyte [5];      // we don't need the place for string terminator in the byte array
memcpy(mybyte,whatever,5);

Please do not post screenshots of error messages. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags.

char, byte and uint8_t all refer to an 8 bit value

Reading that makes me think that you have an array of chars like

char charArray[] = {'1', '2', '3'};

and want to convert it or copy it to an array of bytes like

byte byteArray[] = {1, 2, 3};

If that is not what you are trying to do then please provide an example of before and after as above

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