conversion of uint8_t to a string [C]

给你一囗甜甜゛ 提交于 2019-12-05 20:19:39

1.) it's a little bit faster to eliminate the int array.

2.) adding '0' changes the integer values 0 and 1 to their ascii values '0' and '1'.

3.) it's undefined behaviour to return the address of a local variable. You have to malloc memory in the heap.

4.) yes, just cut it out and do the whole operation all in one

#include <stdio.h>
#include <stdlib.h>

typedef unsigned char uint8_t;

char *convert(uint8_t *a)
{
  char* buffer2;
  int i;

  buffer2 = malloc(9);
  if (!buffer2)
    return NULL;

  buffer2[8] = 0;
  for (i = 0; i <= 7; i++)
    buffer2[7 - i] = (((*a) >> i) & (0x01)) + '0';

  puts(buffer2);

  return buffer2;
}


int main()
{
  uint8_t example = 0x14;
  char *final_string;

  final_string = convert(&example);
  if (final_string)
  {
    puts(final_string);

    free(final_string);
  }
  return 0;
}

Here's one way ...

char *uint8tob( uint8_t value ) {
  static uint8_t base = 2;
  static char buffer[8] = {0};

  int i = 8;
  for( ; i ; --i, value /= base ) {
    buffer[i] = "01"[value % base];
  }

  return &buffer[i+1];
}

char *convert_bytes_to_binary_string( uint8_t *bytes, size_t count ) {
  if ( count < 1 ) {
    return NULL;
  }

  size_t buffer_size = 8 * count + 1;
  char *buffer = calloc( 1, buffer_size );
  if ( buffer == NULL ) {
    return NULL;
  }

  char *output = buffer;
  for ( int i = 0 ; i < count ; i++ ) {
    memcpy( output, uint8tob( bytes[i] ), 8 );
    output += 8;
  }

  return buffer;
};

int main(int argc, const char * argv[]) {
  uint8_t bytes[4] = {  0b10000000, 0b11110000, 0b00001111, 0b11110001 };

  char *string = convert_bytes_to_binary_string( bytes, 4 );
  if ( string == NULL ) {
    printf( "Ooops!\n" );
  } else {
    printf( "Result: %s\n", string );
    free( string );
  }

  return 0;
}

... just extend for 16 bytes. There are many ways and it also depends on what do you mean with quick. Embedded systems, ...? You can make translation table to make it even faster, ...

UPDATE

char *convert_bytes_to_binary_string( uint8_t *bytes, size_t count ) {
  if ( count < 1 ) {
    return NULL;
  }

  const char *table[] = {
    "0000", "0001", "0010", "0011",
    "0100", "0101", "0110", "0111",
    "1000", "1001", "1010", "1011",
    "1100", "1101", "1110", "1111"
  };

  size_t buffer_size = 8 * count + 1;
  char *buffer = malloc( buffer_size );
  if ( buffer == NULL ) {
    return NULL;
  }

  char *output = buffer;
  for ( int i = 0 ; i < count ; i++ ) {
    memcpy( output, table[ bytes[i] >> 4 ], 4 );
    output += 4;
    memcpy( output, table[ bytes[i] & 0x0F ], 4 );
    output += 4;
  }

  *output = 0;

  return buffer;
};

int main(int argc, const char * argv[]) {
  uint8_t bytes[4] = {  0b10000000, 0b11110000, 0b00001111, 0b11110001 };

  char *string = convert_bytes_to_binary_string( bytes, 4 );
  if ( string == NULL ) {
    printf( "Ooops!\n" );
  } else {
    printf( "Result: %s\n", string );
    free( string );
  }

  return 0;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!