;
执行效果:
代码2:字符串转十六进制字符串函数
1 #include<stdio.h>
2 #include<string.h>
3 //字符串转十六进制字符串
4 void Str2Hex( const char *sSrc, char *sDest, int nSrcLen )
6 int i;
7 char szTmp[3];
9 for( i = 0; i < nSrcLen; i++ )
10 {
11 sprintf( szTmp, "%02X", (unsigned char) sSrc[i] );
12 memcpy( &sDest[i * 2], szTmp, 2 );
13 }
14 return ;
15 }
16 int main(int argc, char const *argv[])
17 {
18 char source[100]="Hello world!\0";
19 char dest[100]={0};
20 Str2Hex(source,dest,strlen(source));
21 puts(dest);
22 return 0;
执行效果:
代码3:二进制字符串到十六进制字符串函数
1 #include <stdio.h>
2 #include<string.h>
3 void Bin2Hex(const char *sSrc, char *sDest, int nSrcLen){
4 int times=nSrcLen/4;
5 char temp[times];
6 int x=0;
7 for(int i=0;i<times;i++){
8 //int num=8*int(sSrc[i*4])+4*int(sSrc[i*4+1])+2*int(sSrc[i*4+2])+int(sSrc[i*4+3]);
9 x=8*(sSrc[i*4]-'0');
10 x+=4*(sSrc[i*4+1]-'0');
11 x+=2*(sSrc[i*4+2]-'0');
12 x+=sSrc[i*4+3]-'0';
13 sprintf(temp+i,"%1x",x);
14 }
15 memcpy(sDest,temp,times);
16 }
17 int main()
18 {
19 char Hexstring[200]={0};
20 char Binarystring[600]="010001100110110001101111011101110110010101110010011100110010000001100001011100100110010100100000011100110110111100100000011010010110111001100011011011110110111001110011011010010111001101110100011001010110111001110100001000010010000001000010011101010111010000100000010010010010000001110111011000010111001100100000011101000110111101101111001000000111100101101111011101010110111001100111001000000111010001101111001000000110101101101110011011110111011100100000011010000110111101110111001000000111010001101111001000000110110001101111011101100110010100100000011010000110010100101110";
21 Bin2Hex(Binarystring,Hexstring,strlen(Binarystring));
22 printf("二进制串长度:%d,二进制串:%s\n十六进制串长度:%d,十六进制串:%s\n",strlen(Binarystring),Binarystring,strlen(Hexstring),Hexstring);
23 return 0;
执行效果:
1 #include<stdio.h>
2 #include<string.h>
3 //十六进制字符串转二进制字符串
4 void Hex2Bin(char *source,char *dest,int len)
6 int i=0;
7 char Dict[17][5] =
9 "0000", "0001", "0010", "0011",
10 "0100", "0101", "0110", "0111",
11 "1000", "1001", "1010", "1011",
12 "1100", "1101", "1110", "1111",
13 };
14 for(i=0;i<len;i++){
15 //char temp[5]={0};
16 int n = 16;
17 if (source[i] >= 'a' && source[i] <= 'f') n = source[i] - 'a' + 10;
18 if (source[i] >= 'A' && source[i] <= 'F') n = source[i] - 'A' + 10;
19 if (source[i] >= '0' && source[i] <= '9') n = source[i] - '0';
20 //sprintf(temp,"%s", Dict[n]);
21 //memcpy(&dest[i*4],temp,4);
22 memcpy(&dest[i*4],Dict[n],4);
23 }
24 return;
25 }
27 int main()
28 {
29 char Hexstring[64]="14a57bcf39\0";
30 char Binarystring[256]={0};
31 Hex2Bin(Hexstring,Binarystring,strlen(Hexstring));
32 printf("十六进制串长度:%d十六进制串:%s,\n二进制串长度:%d,二进制串:",strlen(Hexstring),Hexstring,strlen(Binarystring));
33 puts(Binarystring);
34 return 0;
执行效果:
代码5:最后,来一个等长二进制字符串异或函数
1 #include<stdio.h>
2 #include<string.h>
3 //等长比特字符串异或
4 int OZ_bin_xor( const char *s1, char *s2, char *dest)
6 int i;
7 int temp1=0,temp2=0,temp3=0;
8 if(strlen(s1)!=strlen(s2))
10 printf("错误,不等长!\n");
11 return 1;
12 }
13 for( i = 0; i < strlen(s1); i++ )
14 {
15 temp1=s1[i]-'0';
16 temp2=s2[i]-'0';
17 temp3=temp1^temp2;
18 if(temp3==1)
19 dest[i]='1';
20 else if(temp3==0)
21 dest[i]='0';
22 else{
23 printf("字符串内容有误!\n");
24 return 1;
25 }
26 }
27 return 0;
28 }
29 int main(int argc, char const *argv[])
30 {
31 char s1[10]="1001011\0";
32 char s2[10]="0111010\0";
33 char s3[10]={0};
34 if(OZ_bin_xor(s1,s2,s3)!=0)
35 {
36 printf("函数出错!\n");
37 return 1;
38 }
39 puts(s1);
40 puts(s2);
41 puts(s3);
42 return 0;
执行效果:
Have Fun!