数据转换-位串字节数组
作者:互联网
#include <stdio.h> #include <string.h> #include "utils.h" char Hex2Char(int i){ if(i>=0 && i<= 9) return i + 0x30; // return i + '0' if(i>=10 && i<=15) return i + 0x37; //return i + 'A' - 10; } int Char2Hex(char x){ if(x>48&&x<58) return x-48; if(x>65&&x<75) return x - 55; } void B2Arr(char bs[],char ba[]) { printf("1"); int b1,b2,b3,b4,b5,i; for(i = 0;i<strlen(bs);i+4) {b1 = Char2Hex(bs[i]); b2 = Char2Hex(bs[i + 1]); b3 = Char2Hex(bs[i+2]); b4 = Char2Hex(bs[i + 3]); b5 = b1*8 + b2*4 + b3*2 + b4*1; ba[i/4] = Hex2Char(b5); } printf("%s转化为%s\n",bs,ba); return 0; }
void ByteArr2Bitstr(char ba[],char bs[])
{
int b1,b2,b3,b4,b5,i;
for (i = 0;i<strlen(ba);i++)
{b5 = Char2Hex(ba[i]);
b1 = b5/8;
b2 = (b5-b1*8)/4;
b3 = (b5-b1*8 -b2*4)/2;
b4 = b5%2;
bs[i*4] = Hex2Char(b1);
bs[i*4+1] = Hex2Char(b2);
bs[i*4+2] = Hex2Char(b3);
bs[i*4+3] = Hex2Char(b4);
}
printf("%s转化为%s\n",ba,bs);
}
#include <stdio.h>
#include "utils.h"
void main(){
char ba1[] = "1316";
char bs1[] = "1100110000110100";
char bs2[100];
char ba2[100];
ByteArr2Bitstr(ba1,bs2);
}
标签:字节,位串,char,b5,b1,数组,bs,include,Hex2Char 来源: https://www.cnblogs.com/abcd123123123/p/14766002.html