这是自己写的
void BitToHex(ElemType bit[4],ElemType *hex)
{
*hex=bit[3]+bit[2]*2+bit[1]*4+bit[0]*8;
}
void HexToBit(ElemType hex,ElemType bit[4])
{
bit[0]=hex/8;
bit[1]=hex%8/4;
bit[2]=hex%4/2;
bit[3]=hex%2;
}
字节
//字节转换成二进制
void ByteToBit(ElemType ch, ElemType bit[8]){
int cnt;
for(cnt = 0;cnt < 8; cnt++){
*(bit+cnt) = (ch>>cnt)&1;
}
}
//二进制转换成字节
void BitToByte(ElemType bit[8],ElemType *ch){
int cnt;
for(cnt = 0;cnt < 8; cnt++){
*ch |= *(bit + cnt)<<cnt;
}
}
字符串
//将长度为8的字符串转为二进制位串
void Char8ToBit64(ElemType ch[8],ElemType bit[64]){
int cnt;
for(cnt = 0; cnt < 8; cnt++){
ByteToBit(*(ch+cnt),bit+(cnt<<3));
}
}
//将二进制位串转为长度为8的字符串
void Bit64ToChar8(ElemType bit[64],ElemType ch[8]){
int cnt;
memset(ch,0,8);
for(cnt = 0; cnt < 8; cnt++){
BitToByte(bit+(cnt<<3),ch+cnt);
}
}