头文件h
点击查看代码
int ByteArr2Bitstr(int ba,int * bs);
int Bitstr2ByteArr(int * bs,int ba);
![image](https://img2023.cnblogs.com/blog/2166633/202305/2166633-20230524091709277-1958877770.png)
主函数
点击查看代码
char HStr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
int ByteArr2Bitstr(int ba,int * bs)
{
for (int i =0 ; i<8; ++i)
{
int nTmp = (1 << i);
bs[7-i] = (( ba & nTmp) == nTmp )? 1 : 0;
}
}
int Bitstr2ByteArr(int * bs,int ba)
{
int n = 0;
for (int i = 0 ; i < 8; ++i)
{
n += bs[7-i] * (1 << i);
}
*bs = n;
return n;
}
int main()
{
int bitList[8] = { 0 };
int a;
sscanf("27", "%x", &a);
ByteArr2Bitstr(a, bitList);
printf("0x27 be changed to\n");
for (int i =0 ; i<8; ++i)
printf("%i",bitList[i]);
printf("\n");
int b,n;
n=Bitstr2ByteArr(bitList,b);
printf("the data above be changed to\n");
printf("%x\n",n);
}
![image](https://img2023.cnblogs.com/blog/2166633/202305/2166633-20230524092624203-1732342984.png)
![image](https://img2023.cnblogs.com/blog/2166633/202305/2166633-20230524092701887-1568421216.png)