0

What would be a way to convert an array like this

int bit_array[8] = {0,0,0,0,0,0,0,0};

into a char? (assuming bit_array is in ascii or similar)

for example an end result would preferably be like this:

int bit_array[8] = {0,1,1,0,1,0,0,0}; // h
int bit_array2[8] = {0,1,1,0,1,0,0,1}; // i
char byte;
char byte2;
byte = funny_function(bit_array);
byte2 = funny_function(bit_array2);
printf("%s%s",byte,byte2); //out: "hi"
2
  • You might cast integers to bits then left shift a byte which has initial value 0 with the array elements. Then you would simply return that byte. Commented Aug 26, 2022 at 1:31
  • printf("%s%s",byte,byte2); //out: "hi" Characters are not strings. Use %c. Commented Aug 26, 2022 at 7:36

3 Answers 3

1

printf("%s%s",byte,byte2); //out: "hi" will not printf anything and is wrong. %s expects a pointer to char referencing null character terminated C string and you pass integers (char is an integer).

It has to be:printf("%c%c",byte,byte2);

int funny_function(const int *bit_array)
{
    int result = 0;
    for(int i = 0; i < 8; i++)
    {   
        result <<= 1;
        result += !!bit_array[i];
    }
    return result;
}

in non zero bit_array element value is considered 1.

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

2 Comments

did you mean result += bit_array[i]? You should not increment i.you do not need !!. Also, bitwise arithmetic might be a bit better. Also, checking array size would be a good idea.
@Serge indeed. Late night
0

Without using a function call, you can do this with a loop in the body of main().

int main( void ) {
    // Only need 7 'bits'.
    // Let compiler measure size.
    // Use 1 byte 'char' instead of multibyte 'int'
    char bits[][7] = {
        {1,1,0,1,0,0,0}, // h
        {1,1,0,0,1,0,1}, // e
        {1,1,0,1,1,0,0}, // l
        {1,1,0,1,1,0,0}, // l
        {1,1,0,1,1,1,1}, // o
    };

    // for each character row of array
    // Simply use a loop, not a function
    for( int i = 0; i < sizeof bits/sizeof bits[0]; i++ ) {
        char c = 0;

        // for each bit of each character
        // 7 bit values are 'accumulated' into 'c'...
        for( int ii = 0; ii < sizeof bits[0]/sizeof bits[0][0]; ii++ )
            c = (char)( (c << 1) | bits[i][ii] ); // Magic happens here

        putchar( c );
    }
    putchar( '\n' );

    return 0;
}

(Friendly) output:

hello

As above, an alternative version if output limited to only single case alphabet (fewer bits).

int main( void ) {
    // Only need 5 'bits'.
    char low_bits[][5] = {
        {0,1,0,0,0}, // h
        {0,0,1,0,1}, // e
        {0,1,1,0,0}, // l
        {0,1,1,0,0}, // l
        {0,1,1,1,1}, // o
    };

    for( int i = 0; i < sizeof low_bits/sizeof low_bits[0]; i++ ) {
        char *cp = low_bits[i]; // for clarity (brevity)

        // Magic happens here...
        char c = (char)(cp[0]<<4 | cp[1]<<3 | cp[2]<<2 | cp[3]<<1 | cp[4] );

        putchar( c | 0x40); // uppercase. use 0x60 for all lowercase.
    }
    putchar( '\n' );

    return 0;
}

(Not so friendly) output (unless calling to distant person):

HELLO

1 Comment

Thankyou! I only had the function because I couldn't think of anything better to put
0

Assuming that you are certain that each item contains either 1 or 0, then it's as simple as this:

char funny_function (const int array[8])
{
  char result = 0;
  for(size_t i=0; i<8; i++)
  {
    result |= array[i] << (7-i);
  }
  return result;
}

That is:

  • Take the 1st array item of 0,1,1,0,1,0,0,0, left shift it 7 bits = 0x00, OR with 0x00 == 0x00.
  • Take the 2nd array item of 0,1,1,0,1,0,0,0, left shift it 6 bits = 0x40, OR with 0x00 == 0x40.
  • Take the 3rd array item of 0,1,1,0,1,0,0,0, left shift it 5 bits = 0x20, OR with 0x40 == 0x60.
  • And so on.

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.