Pages: [1]   Go Down
Author Topic: help!!byte array into string?  (Read 301 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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'}
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
Logged

Pages: [1]   Go Up
Jump to: