how to convert string to byte ?
eg : string "60" to byte 0x60
eg : string "a5" to byte 0xa5
thx in advance
how to convert string to byte ?
eg : string "60" to byte 0x60
eg : string "a5" to byte 0xa5
thx in advance
Use strtoul(). Set the base to 16, and you can just pass in NULL for the endptr argument, like so:
char s[] = "60";
byte b = strtoul(s, nullptr, 16);
// b is now 0x60
okay and how to convert string to char[] ?
eg
String a = "60" ;
to ...
String test = "60";
char charbuf[2];
test.toCharArray(charbuf, 8);
byte b = strtoul(s, nullptr, 16);
---error = exit status 1
's' was not declared in this scope
char charbuf[2];
is not big enough to contain your string
You don't need to copy the String to a string. Use c_str()
to get to the String's internal string buffer:
String test = "60";
byte b = strtoul(test.c_str(), nullptr, 16);
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.