0

I have storage a "bits" in integer array for example

int a = {1,0,1,1} 

How I will be able to convert the integer array "bits" into unsigned char "bits"?

and reciprocal?

3
  • What do you mean with "integer array bits"? Commented Aug 27, 2012 at 23:59
  • 1
    not clear for me. Do you want to convert {1,0,1,1} to unsigned char e.g, unsigned char x = 1011; or its binary representation? Commented Aug 28, 2012 at 0:25
  • Your sample shouldn't compile without a severe warning, if at all. Commented Aug 28, 2012 at 8:24

1 Answer 1

3
int int_array[] = {1, 0, 1, 1};
unsigned char uc_array[sizeof int_array / sizeof *int_array];
for (size_t i = 0; i != sizeof int_array / sizeof *int_array; ++i)
  uc_array[i] = int_array[i];

(Your sample code is flawed: it has too many initializers for a scalar variable)

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

2 Comments

"Your sample code is flawed: it has too many initializers for a scalar variable" not understand, why is wrong?
@Juan: Because it should be int a[].

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.