Created
May 31, 2018 18:44
-
-
Save dlwhitehurst/6aff1dc85205315e59d5b60e702681b2 to your computer and use it in GitHub Desktop.
Convert unsigned char array to string
This file contains hidden or 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
#define bufferSize 10 | |
int main() { | |
unsigned char buffer[bufferSize]={1,2,3,4,5,6,7,8,9,10}; | |
char converted[bufferSize*2 + 1]; | |
int i; | |
for(i=0;i<bufferSize;i++) { | |
sprintf(&converted[i*2], "%02X", buffer[i]); | |
/* equivalent using snprintf, notice len field keeps reducing | |
with each pass, to prevent overruns | |
snprintf(&converted[i*2], sizeof(converted)-(i*2),"%02X", buffer[i]); | |
*/ | |
} | |
printf("%s\n", converted); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment