如何将 uint8_t 数组转换为 C 中的字符数组?

How to convert a uint8_t array to a character array in C?

我有一个 uint8_t 数组,我希望能够将其转换为 char 数组。 即 uint_8 myInput [4] = {0, 233, 3, 1};

对应的位数:00000000 00000011 11101001 00000001

我想要一个字符数组:char myChar [10] ={2,5,6,2,5,7}; 这是等效的数字。

我能够创建一个解决方案:MySolution:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>     /* strtol */
#include <stdint.h>
#include <string.h>

int my_uint8_t_to_char (uint8_t givenData[]);


int main ()
    {
    
      uint8_t testArray[4];
      uint32_t monitor_counter;
      char sendBuffer[10];
      monitor_counter = 256257;
    
    // Here we create our array of uint8_t
      memcpy (testArray, &monitor_counter, sizeof (monitor_counter));
    
    // We flip the order of the bits
      for (int i = 0; i < 4 / 2; i++)
        {
          int temp = testArray[i];
          testArray[i] = testArray[4 - 1 - i];
          testArray[4 - 1 - i] = temp;
        }
    
    //Call the function that converts the uint8_t to an integer value
    int beforeSend = my_uint8_t_to_char(testArray);
    printf ("\nHere it is: %d",beforeSend);
   
    
    sprintf(sendBuffer, "%d", beforeSend);
    // for ( int i=0; i< 10;i++)
    printf("\nConverted character: %s",sendBuffer);
      
      
    
      return 0;
    }
    

int my_uint8_t_to_char (uint8_t givenData[])
    {
    
      char biggerArrayToSaveOctades[32];
      int n, c, k, i, w;
      unsigned int arr1[8];
      int arrTemp[8];
      long long bigBinaryString;
    
    
    
      int j;
      int index;
      const int length_A = 8;
      
      char astr[length_A*10];
      char astr2[length_A*10];
      char astr3[length_A*10];
      char astr4[length_A*10];
      printf("\nSIZE: %d",sizeof(astr4));
    
      // In every loop we get one of the 4 integers from the uint8_t array and convert it to binary character 
      for (i = 0; i < 4; i++)
        {

          n = givenData[i];
    
          for (c = 7; c >= 0; c--)
        {
          k = n >> c;
    
          if (k & 1)
            {
              arr1[c] = 1;
            }
          else
            {
              arr1[c] = 0;
            }
        }
    
    
          /*Flip the bits order */
          for (int w = 0; w < 8 / 2; w++)
        {
          int temp = arr1[w];
          arr1[w] = arr1[8 - 1 - w];
          arr1[8 - 1 - w] = temp;
        }

    
    // in each case we append into the biggerArrayToSaveOctades the bitstrings
          switch (i)
        {
    
        case 0:
    
          j = 0;
          index = 0;
    
          for (j = 0; j < length_A; j++)
            {
              index += sprintf (&astr[index], "%d", arr1[j]);
            }
    
          for (w = 0; w < 8; w++)
            biggerArrayToSaveOctades[w] = astr[w];
    
          break;
    
        case 1:
          j = 0;
          index = 0;
    
          for (j = 0; j < length_A; j++)
            {
              index += sprintf (&astr2[index], "%d", arr1[j]);
            }
    
          j = 0;
          for (w = 8; w < 16; w++)
            {
              biggerArrayToSaveOctades[w] = astr2[j];
              j++;
            }
    
          break;
    
        case 2:
          j = 0;
          index = 0;
    
          for (j = 0; j < length_A; j++)
            {
              index += sprintf (&astr3[index], "%d", arr1[j]);
            }
    
          j = 0;
          for (w = 16; w < 24; w++)
            {
              biggerArrayToSaveOctades[w] = astr3[j];
              j++;
            }
    
          break;
    
        case 3:
          j = 0;
          index = 0;
    
          for (j = 0; j < length_A; j++)
            {
              index += sprintf (&astr4[index], "%d", arr1[j]);
            }
    
          j = 0;
          for (w = 24; w < 32; w++)
            {
              biggerArrayToSaveOctades[w] = astr4[j];
              j++;
            }
          break;
        }
    
    
        }
     //Convert the bitstring to integer
      int result = (int) strtol (biggerArrayToSaveOctades, NULL, 2);
    
      return result;
    }

但不幸的是,我在使用 ISO 创建 char astr[length_A*10]; 时遇到了一些问题,因为我使用的是物联网设备,而且我认为我的解决方案非常复杂且效率不高。

有什么办法吗?

字节代表以下之一:

  • 32 位无符号整数
  • 32 位 2 的补码有符号整数
  • 32 位 1 的补码有符号整数(极不可能)

正在使用非常奇怪的字节顺序:

  • 第一个最重要
  • 第三个是次重要的
  • 第二个是次重要的
  • 第四个是最不重要的

这既不是大端字节序也不是小端字节序。

首先将字节转换为适当的数字,然后将结果数字转换为其十进制文本表示形式(例如使用 sprintf/snprintf)。


32 位无符号整数

转换为数字:

#include <stdint.h>

uint32_t num = 
   ( (uint32_t)(myInput[0]) << 24 ) |
   ( (uint32_t)(myInput[2]) << 16 ) |
   ( (uint32_t)(myInput[1]) <<  8 ) |
   ( (uint32_t)(myInput[3]) <<  0 );

对于您的输入,这相当于

uint32_t num = 256257;

转换为十进制表示:

#include <inttypes.h>
#include <stdio.h>

char decimal[11];  // Up to 10 digits plus final NUL.
sprintf(decimal, "%" PRIu32, num);

对于您的输入,这相当于

char decimal[11] = { '2', '5', '6', '2', '5', '7', 0 };

测试:

#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>

typedef uint8_t uint_8;  // Why not just use uint8_t??

int main(void) {
   uint_8 myInput[4] = {0, 233, 3, 1};

   uint32_t num =
      ( (uint32_t)(myInput[0]) << 24 ) |
      ( (uint32_t)(myInput[2]) << 16 ) |
      ( (uint32_t)(myInput[1]) <<  8 ) |
      ( (uint32_t)(myInput[3]) <<  0 );

   char decimal[11];  // Up to 10 digits plus final NUL.
   sprintf(decimal, "%" PRIu32, num);

   // If all you want to do is print the number,
   // `printf("%" PRIu32, num);` would do the trick.
   printf("%s\n", decimal);  // 256257
}