String To ByteArray In Arduino - Tutorialspoint
Maybe your like
- Home
- Whiteboard
- Online Compilers
- Practice
- Articles
- AI Assistant
- Jobs
- Tools
- Corporate Training
- Courses
- Certifications
- Switch theme
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
The getBytes() function helps copy the content of a String to a byte array. The syntax is −
string1.getBytes(buf,len)where,
string1 is the string whose content you want to copy to a byte array,
buf is the byte array, and
len is the length of content to be copied.
Example
The following example illustrates how to use this function −
byte buf[10]; void setup() { Serial.begin(9600); Serial.println(); String s1 = "Hello World"; s1.getBytes(buf, 5); for (int i = 0; i < 10; i++) { Serial.println(buf[i]); } } void loop() { }Output
The Serial Monitor output is shown below −

As you can see, the characters have been copied into the byte array. 72 corresponds to the decimal value of 'H' as per the ASCII code, 101 corresponds to the decimal value of 'e' and so on.
Note that, although the value for len was specified as 5, the bytes of only 4 characters were copied. This indicates that the last byte is intentionally kept as 0 to indicate string termination. You can try setting other values for len, and you'll get a similar result. The last byte will be kept as 0.
Yash Sanghvi Updated on: 2021-07-24T14:07:44+05:30 9K+ Views
Kickstart Your Career
Get certified by completing the course
Get StartedTag » Arduino Write Byte Array
-
Serial, Sending Entire Byte Array - Networking, Protocols, And Devices
-
Serial.write() - Arduino Reference
-
Sending Byte Array Via Serial - Arduino Forum
-
How To Write To Serial A Byte Array With Dynamic Array? - Arduino Forum
-
Print And Write Data Using 13 Byte Array - Arduino Forum
-
Write/read Byte Array To SD - Programming Questions - Arduino Forum
-
Write Bytes To And Read Bytes From Arduino
-
Serial.write() | Référence Du Langage Arduino En Français
-
Simple Updated Examples Of Arduino Serial Communications · GitHub
-
Return Byte Array With Arduino - Stack Overflow
-
Arduino Byte Array
-
Serial.Write(buf,Len) How To Read Serial Sended Array Of Byte
-
Arduino Store Array Into EEPROM - The Robotics Back-End
-
Arduino How To Print Byte Array Code Example - Code Grepper