Created
February 23, 2020 06:56
-
-
Save rosmianto/44dbfd3ccc136ea64eacc58cd75479e0 to your computer and use it in GitHub Desktop.
How to Convert Byte Array to Hexstring Arduino Platform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
veryyyy thank you!