std::string char* unsigned char* uint8_t int8_t int argc char* argv[]关系

主要指针能代替数组使用
https://blog.csdn.net/liusicheng2008_liu/article/details/80412586

char ** 与char * a[ ] ;

        先看 char  *a [ ] ;

        由于[ ] 的优先级高于* 所以a先和 [ ]结合,他还是一个数组,数组中的元素才是char * ,前面讲到char * 是一个变量,保存的地址。。

        所以 char *a[ ] = {"China","French","America","German"};

        同过这句可以看到, 数组中的元素是字符串,那么sizeof(a) 是多少呢,有人会想到是五个单词的占内存中的全部字节数 6+7+8+7 = 28;

        但是其实sizeof(a) = 16;

        为什么,前面已经说到, 字符串常量的本质是地址,a 数组中的元素为char * 指针,指针变量占四个字节,那么四个元素就是16个字节了
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;
}
sizeof(char**)sizeof(char*), sizeof(char)小注
sizeof(char) → 返回char型所占空间:1 (Byte)

sizeof(char*) → 返回char*型指针所占空间:4 (Byte)

sizeof(数组名) → 返回该字符串指针数组里元素所占空间:n*4(8)(n为字符串指针数组元素个数,也即数组的字符串个数)

 

比如 char *strlist[] = {"American", "Germany", "Japan", "China", "France", "Russia"}

此时sizeof(strlist) = 6*4 = 24(Byte),代表着6个char*类型的总大小。

所以想求的字符串指针数组的字符串元素个数就可以用 sizeof(strlist)/sizeof(char*)来得到。

测试

    char *strlist[] = {"American", "Germany", "Japan", "China", "France", "Russia"};
    printf("sizof(int):%d\n",sizeof(int));
    printf("sizof(char *):%d\n",sizeof(char *));
    printf("sizof(char ):%d\n",sizeof(char ));
    printf("sizeofstrlist%d, strlenstrlist:%d\n",sizeof(strlist),strlen(strlist));
	 printf("%s\n",strlist);

结果

sizof(int):4
sizof(char *):8
sizof(char ):1
sizeofstrlist48, strlenstrlist:3
q@@

不能用strlen 来计算 字符指针数组???

if ((err = snd_pcm_hw_params(capture_handle, hw_params)) < 0) { printf("Error setting parameters: %s\n", snd_strerror(err)); return 1; } snd_pcm_hw_params_free(hw_params); unsigned int frames_per_period = 2048; snd_pcm_uframes_t period_size = frames_per_period * CHANNELS * 2; snd_pcm_uframes_t buffer_size = period_size * 2; if ((err = snd_pcm_set_params(capture_handle, FORMAT, SND_PCM_ACCESS_RW_INTERLEAVED, CHANNELS, rate, 1, frames_per_period)) < 0) { printf("Error setting parameters: %s\n", snd_strerror(err)); return 1; } FILE *file = fopen(argv[1], "wb"); if (!file) { printf("Error opening file for writing!\n"); return 1; } uint32_t chunk_size = 0; uint32_t subchunk_size = 16; uint16_t audio_format = 1; uint16_t num_channels = CHANNELS; uint32_t sample_rate = rate; uint32_t byte_rate = (rate * CHANNELS * 2); uint16_t block_align = (CHANNELS * 2); uint16_t bits_per_sample = 16; uint32_t data_size = 0; fwrite("RIFF", 1, 4, file); fwrite(&chunk_size, 4, 1, file); fwrite("WAVE", 1, 4, file); fwrite("fmt ", 1, 4, file); fwrite(&subchunk_size, 4, 1, file); fwrite(&audio_format, 2, 1, file); fwrite(&num_channels, 2, 1, file); fwrite(&sample_rate, 4, 1, file); fwrite(&byte_rate, 4, 1, file); fwrite(&block_align, 2, 1, file); fwrite(&bits_per_sample, 2, 1, file); fwrite("data", 1, 4, file); fwrite(&data_size, 4, 1, file); uint16_t buffer[frames_per_period * CHANNELS]; while (1) { int n = snd_pcm_readi(capture_handle, buffer, frames_per_period); if (n < 0) { printf("Error reading from PCM device: %s\n", snd_strerror(n)); break; } fwrite(buffer, 2, n * CHANNELS, file); data_size += n * CHANNELS * 2; } uint32_t file_size = data_size + 36; fseek(file, 4, SEEK_SET); fwrite(&file_size, 4, 1, file); fseek(file, 40, SEEK_SET); fwrite(&data_size, 4, 1, file); fclose(file); snd_pcm_close(capture_handle); 加上注释
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值