Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlwhitehurst/6aff1dc85205315e59d5b60e702681b2 to your computer and use it in GitHub Desktop.
Save dlwhitehurst/6aff1dc85205315e59d5b60e702681b2 to your computer and use it in GitHub Desktop.
Convert unsigned char array to string
#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