0

I have an int array of 8 positions representing 8 bits.

int zBits[8]={0, 1, 1, 1, 1, 0, 1, 0};

zBits array is the character "z" in bits ("7a" in hex) (http://www.utf8-chartable.de/).

How can I convert this int array (zBits) in the "z" char using c++?

1 Answer 1

4

The most straightforward way to do this is directly:

char z = (zBits[7]     ) |
         (zBits[6] << 1) |
         (zBits[5] << 2) |
         (zBits[4] << 3) |
         (zBits[3] << 4) |
         (zBits[2] << 5) |
         (zBits[1] << 6) |
         (zBits[0] << 7);

If you like, you can reformulate this into a loop which will be slightly shorter.

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

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.