Go Down

Topic: help!!byte array into string? (Read 958 times) previous topic - next topic


i want to convert a byte array into its ascii characters for example:
if my byte array is
byte array[]={0x23,0xda,0xbc}  ;        i want to store
char array[]={'2','3','d','a','b','c'}


This is quite easy. Just call hexify with the two arrays and ensure that the output buffer is big enough!

Code: [Select]
uint8_t hex_nibble(uint8_t v) {
  if (v < 10) return '0' + v;
  return 'A' - 10 + v;

void hexify(uint8_t *in, uint8_t *out, uint8_t len) {
  for (uint8_t i = 0; i < len; i++) {
    *(out++) = hexnibble(in[i] >> 4);
    *(out++) = hexnibble(in[i] & 0x0F);

Disclaimer: Code is untested, just a very quick hack.

Go Up