C语言 uint8_t 到字符串的转换 [C]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27448168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
conversion of uint8_t to a string [C]
提问by Antonino
I'm trying to "translate" an array of uint8_t [uint8_t lets_try[16]] to a string of 16*8+1[null character] elements. For example:
我正在尝试将 uint8_t [uint8_tlets_try[16]] 数组“翻译”为 16*8+1[null character] 元素的字符串。例如:
lets_try[0] = 10101010
lets_try[1] = 01010101
...
...
and I would like to have a string like:
我想要一个像这样的字符串:
1010101001010101...[\0]
1010101001010101...[\0]
Here the questions: 1) is there a quick way to perform this operation?
这里的问题是:1)有没有一种快速的方法来执行这个操作?
I was trying to do it on my own; my idea was starting from translating a single uint8_t variable into a string and obtaining the full array with a loop [I haven't done this last part yet]. At the end I wrote this code:
我试图自己做;我的想法是从将单个 uint8_t 变量转换为字符串并使用循环获取完整数组开始[我还没有完成最后一部分]。最后我写了这段代码:
int main()
{
uint8_t example = 0x14;
uint8_t *pointer;
char *final_string;
pointer = &example;
final_string = convert(pointer);
puts(final_string);
return(0);
}
char *convert (uint8_t *a)
{
int buffer1[9];
char buffer2[9];
int i;
char *buffer_pointer;
buffer1[8]='#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;
}
';
for(i=0; i<=7; i++)
buffer1[7-i]=( ((*a)>>i)&(0x01) );
for(i=0; i<=7; i++)
buffer2[i] = buffer1[i] + '0';
buffer2[8] = '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;
}
';
puts(buffer2);
buffer_pointer = buffer2;
return buffer_pointer;
}
Here other few questions:
这里还有几个问题:
2) I'm not sure I fully understand the magic in this expression I found online: buffer2[i] = buffer1[i] + '0'; can somebody explain to me why the following puts(buffer2) is not going to work correctly without the +'0'? is it the null character at the end of the newborn string which makes the puts() work? [because with the null character it knows it's printing a real string?]
2)我不确定我是否完全理解我在网上找到的这个表达式的魔力:buffer2[i] = buffer1[i] + '0'; 有人可以向我解释为什么下面的 puts(buffer2) 在没有 +'0' 的情况下不能正常工作吗?是使 puts() 工作的新字符串末尾的空字符吗?[因为使用空字符它知道它正在打印一个真正的字符串?]
3) in the code above puts(buffer2) gives the right output while the puts in main() gives nothing; I'm going mad in looking again and again the code, I can't find what's wrong with that
3) 在上面的代码中, puts(buffer2) 给出了正确的输出,而 main() 中的 puts 什么也没给出;我一次又一次地查看代码,我快疯了,我找不到那有什么问题
4) in my solution I manage to convert an uint8_t into a string passing from an array of int: uint8_t->int array->string; is there a way to shorten this procedure, passing directly from the uint8_t into a string, or improve it? [in forums I found only solutions in C++] it works but I find it a little heavy and not so elegant
4)在我的解决方案中,我设法将 uint8_t 转换为从 int 数组传递的字符串: uint8_t->int array->string; 有没有办法缩短这个过程,直接从 uint8_t 传递到一个字符串,或者改进它?[在论坛中,我只找到了 C++ 的解决方案] 它可以工作,但我觉得它有点沉重而且不太优雅
Thanks everybody for the support
谢谢大家的支持
回答by mch
1.) it's a little bit faster to eliminate the int array.
1.) 消除 int 数组要快一点。
2.) adding '0'changes the integer values 0and 1to their ascii values '0'and '1'.
2.) 添加'0'更改整数值0和1它们的 ascii 值'0'和'1'。
3.) it's undefined behaviour to return the address of a local variable. You have to malloc memory in the heap.
3.) 返回局部变量的地址是未定义的行为。您必须在堆中分配内存。
4.) yes, just cut it out and do the whole operation all in one
4.) 是的,只需将其剪掉,然后将整个操作全部完成
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;
}
回答by zrzka
Here's one way ...
这是一种方法......
##代码##... 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, ...
...只需扩展 16 个字节。有很多方法,这也取决于您对quick 的含义。嵌入式系统, ...?您可以制作翻译表以使其更快,...
UPDATE
更新
##代码##
