This question already has an answer here:

Well my title pretty much sums it up , how to convert string to byte array? i cant find any usefull code

marked as duplicate by Juraj, KIIV, Majenko arduino-uno Nov 28 at 14:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Well, the C string is already byte array.

If you mean Arduino's String class, then it has c_str() method.

Both needs reinterpret cast to const byte pointer:

byte const* byteArray = reinterpret_cast<byte const*>(yourString.c_str());
  • Yeah the Arduinos String , i did not understand the last line , i am sorry if i post stupid questions and i am new to all of this so i dont have experience – Ivanh23 Nov 28 at 14:02
  • What did you ment by reinterpret cast to const byte pointer? – Ivanh23 Nov 28 at 14:02
  • @Ivanh23 There is a difference between char and byte signedness so it must be cast by reinterpret_cast. I've added it into the answer. But be warned, it's valid only as long as the yourString exists and wasn't changed. If you change the yourString, you have to get correct pointer again. – KIIV Nov 28 at 14:25

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