1

I have an array of data in standard 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)

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?

6
  • 1
    Sorry, just to clarify, you actually want to express binary as decimal? (like express 2 (10) as the actual decimal number 10 (1010))? Commented Jun 11, 2014 at 13:29
  • 1
    I guess these 0's and 1's are random since it doesn't match the hex numbers at all. Commented Jun 11, 2014 at 13:31
  • convert array-datas values into binary. Keep all the element binary values in single buffer. Finally '\0' the buffer. Commented Jun 11, 2014 at 13:34
  • 1
    1st code a function converting a char from its value (0x20 for example) to its bitwise represention, then use strcat() to append thr result to a C-"string". If you face any specific issue in doing so then come back here showing where exactly you got stuck. Commented Jun 11, 2014 at 13:38
  • Hi to clarify I need to convert to data[] array to a binary string. Yes the binary information given above is random Commented Jun 11, 2014 at 13:40

3 Answers 3

2

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

    }
Sign up to request clarification or add additional context in comments.

2 Comments

You can optimize this a bit: temp = datas[loop]; strcat(result, binary[temp >> 4]); strcat(result, binary[temp & 0x0F]);. Still, +1 for the solution.
But why unsigned char? I would simply use char. I don't know nor care if '0' is signed or unsigned. The data can be unsigned chars, but the text is not necessarily.
1

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

Comments

0

You need to convert byte data , or unsigned char to binary, then concatenate the binary strings. 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();
}  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.