Skip to content

Instantly share code, notes, and snippets.

@rosmianto
Created February 23, 2020 06:56
Show Gist options
  • Save rosmianto/44dbfd3ccc136ea64eacc58cd75479e0 to your computer and use it in GitHub Desktop.
Save rosmianto/44dbfd3ccc136ea64eacc58cd75479e0 to your computer and use it in GitHub Desktop.
How to Convert Byte Array to Hexstring Arduino Platform
void setup() {
byte example[] = {0x31, 0x32, 0x33, 0x34};
int length = 4;
String result = "";
String hexstring = "";
for(int i = 0; i < length; i++) {
if(example[i] < 0x10) {
hexstring += '0';
}
hexstring += String(example[i], HEX);
}
result = HexString2ASCIIString(hexstring);
Serial.begin(9600);
Serial.println(result);
}
void loop() {
}
String HexString2ASCIIString(String hexstring) {
String temp = "", sub = "", result;
char buf[3];
for(int i = 0; i < hexstring.length(); i += 2){
sub = hexstring.substring(i, i+2);
sub.toCharArray(buf, 3);
char b = (char)strtol(buf, 0, 16);
if(b == '\0')
break;
temp += b;
}
return temp;
}
@TitanDoctorMachine
Copy link

veryyyy thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment