uint8_t有转换为字符串[C] [英] conversion of uint8_t to a string [C]

查看:9595
本文介绍了uint8_t有转换为字符串[C]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想翻译uint8_t有[uint8_t有lets_try [16]数组为16 * 8 + 1 [空字符]元素的字符串。例如:

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:

lets_try[0] = 10101010  
lets_try[1] = 01010101  

...

和我想有这样的字符串:

and I would like to have a string like:

1010101001010101 ...... [\\ 0]

1010101001010101...[\0]

下面的问题:1)是否有执行此操作的快捷方式

Here the questions: 1) is there a quick way to perform this operation?

我试图做我自己;我的想法是从翻译单一的uint8_t有变量转换成字符串并获得全阵列循环开始[我没有做过这最后一部分还没有。最后我写了这个code:

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:

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]='\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] = '\0';

    puts(buffer2);

    buffer_pointer = buffer2;

    return buffer_pointer;
}

下面的其他几个问题:

2)我不知道我完全理解这个EX pression我在网上找到的法宝:
缓冲器2 [I] =缓冲器1 [I] +'0';为什么下面的看跌期权(缓冲器2)是不会未经+'0'正常工作,有人可以解释一下吗?它是这个初生的字符串,使得看跌期权的末尾的空字符()工作? [因为空字符它知道它的打印真正的字符串?]

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?]

3)code以上的看跌期权(缓冲器2)给出正确的输出,而在main()看跌期权给什么;我在一次又一次的寻找code要疯了,我找不到什么是错

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

4)在我的解决方案我管理一个uint8_t有转换成从int数组传递一个字符串:
uint8_t-> INT阵列 - >串;有没有办法缩短这个过程中,直接从uint8_t有突入串,或改善吗? [论坛中我发现只有解决方案,C ++]它的工作原理,但我觉得有点沉重,不那么优雅

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

谢谢大家的支持。

推荐答案

1)这是一个有点快,消除int数组。

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

2)加入 0 变化的整数值 0 1 来的ASCII值 0 1

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

3)它是不确定的行为返回一个局部变量的地址。你必须在堆内存的malloc

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

4。)是的,只是把它剪尽在一整个操作

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;
}

这篇关于uint8_t有转换为字符串[C]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆