简体   繁体   English

将Char数据数组转换为C中的组合字符串

[英]Convert Char data array to a combined string in C

I have an array of data in standard C 我有一个标准C的数据数组

unsigned char datas[9] = { 0x20,0x01,0x03,0xE0,0X12,0XFF,0,0,0 };

I need to create a binary string of the combined array of this data for example "011001010000101010110110101" (Yes this is a random number but represent what i am trying todo) 我需要创建一个这个数据的组合数组的二进制字符串,例如"011001010000101010110110101" (是的,这是一个随机数,但代表我正在尝试todo)

The string is then passed to another routine for bit stuffing. 然后将该字符串传递给另一个用于位填充的例程。

I'm unsure how to convert the char (datas) to a concatenated binary string? 我不确定如何将char (数据)转换为连接的二进制字符串?

Please refer the below code 请参考以下代码

    #include  "string.h"
    main(int argc, char * * argv)
    {
    unsigned char datas[9] = { 0x20,0x01,0x03,0xE0,0X12,0XFF,0,0,0 };
    int numElements = sizeof (datas)/ sizeof (datas[0]);
     char result [1024] ;
    unsigned char temp =0, loop = 0;
    unsigned char *p;
    unsigned char binary[16][5] = {"0000", "0001", "0010", "0011", "0100", "0101","0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110","1111"};
    result[0] = '\0';
    for (loop=0; loop<numElements; ++loop)
    {
        temp = datas[loop];
        temp = temp>>4;
        p = binary [temp];

        strcat (result,p );
        temp = datas[loop];
        temp = temp&0x0F;
        p = binary [temp];
        strcat (result,p );


    }

    printf ("\n%s\n", result);

    }

You would first need a way to transform an unsigned char to its binary representation - the answers to this question already provide a couple of ideas how to do that. 您首先需要一种方法将unsigned char转换为其二进制表示形式 - 这个问题的答案已经提供了一些如何做到这一点的想法。 You can then allocate the memory for the resulting string in one go and transform each char of your array, as in: 然后,您可以一次为结果字符串分配内存并转换数组的每个字符,如下所示:

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

#define ARRAYLEN(a) (sizeof(a) / sizeof(*a))

static void toBinary(unsigned char ch, char *buf)
{
    size_t i;
    for (i = 0; i < CHAR_BIT; ++i) {
        buf[CHAR_BIT - i - 1] = ch & (1 << i) ? '1' : '0';
    }
}

int main(void)
{
    unsigned char data[] = { 0x20,0x01,0x03,0xE0,0X12,0XFF,0,0,0 };
    char *s;
    size_t i;

    s = calloc(ARRAYLEN(data) * CHAR_BIT + 1, sizeof(*s));
    for (i = 0; i < ARRAYLEN(data); ++i) {
        toBinary(data[i], s + i * CHAR_BIT);
    }

    printf("%s\n", s);
    return 0;
}

You need to convert byte data , or unsigned char to binary, then concatenate the binary strings. 您需要将字节数据或unsigned char转换为二进制,然后连接二进制字符串。 Here is a conversion method for getting the binary string: 这是获取二进制字符串的转换方法:

const char *byte_to_binary32(long x)
{
    static char b[33]; // bits plus '\0'
    b[0] = '\0';
    char *p = b;

    __int64 z;
    //for (z = 2147483648; z > 0; z >>= 1)       //2147483648 == 2^32 
    for (z = 128; z > 0; z >>= 1)                //128 == 2^7 //shows 8 bits of char
    {                                          //(adjust powers of two for length of output)
        *p++ = (x & z) ? '1' : '0';
    }
    return b;
}

int main(void)
{
     int i=0;
     char datas[9] = { 0x20,0x01,0x03,0xE0,0X12,0XFF,0,0,0 };
     char concatStr[100]; //greater than 9 x 8 +1
     //note, data you are reading in may or may not be signed, 
     //do not use unsigned variable to contain it.
     for(i=0;i<sizeof(datas)/sizeof(datas[0]);i++)
     {
         strcat(concatStr, byte_to_binary32((long)datas[i]));
     }
     printf("Binary: %s\n\n", concatStr);
     getchar();
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM