将整数数组转换为unsigned char数组

时间:2012-08-27 23:36:44

标签: c

我在整数数组中存储了一个“位”,例如

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

我怎样才能将整数数组“bits”转换为unsigned char“bits”?

和互惠?

1 个答案:

答案 0 :(得分: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];

(您的示例代码存在缺陷:标量变量的初始化程序太多)