例五:二进制数转换成相应的十进制数[objc] view plain copy
- #include <stdio.h>
- #include <string.h>
-
- #define MAX_SIZE 1024
-
- int bin2int(charchar * bin,int len)
- {
- int i;
- int temp1;
-
- int num = 0;
- int temp = 1;
-
- for(i = len - 1;i >= 0;i --)
- {
- temp1 = *(bin + i) - '0';
- num = num + temp1 * temp;
-
- temp = temp * 2;
- }
-
- return num;
- }
-
- int main()
- {
- int num;
- int len;
-
- char bin[MAX_SIZE];
-
- printf("Please input the binary:\n");
- scanf("%s",bin);
-
- len = strlen(bin);
-
- num = bin2int(bin,len);
-
- printf("The integer of the binary is:%d.\n",num);
-
- return 0;
- }
调试结果为:[objc] view plain copy
- Please input the binary:
- 1101110101011
- The integer of the binary is:7083.
例六:将二进制数转换成相应的十六进制数[objc] view plain copy
- #include <stdio.h>
- #include <string.h>
-
- #define MAX_SIZE 1024
-
- charchar * bin2hex(charchar * bin,int len_bin)
- {
- int i;
- int j;
- int len_hex;
-
- int temp = 1;
- int temp1 = 0;
- int count = 0;
-
- static char hex[MAX_SIZE];
- hex[0] = '0';
- hex[1] = 'x';
-
- if(len_bin % 4 == 0)
- {
- len_hex = len_bin / 4;
- }
- else
- {
- len_hex = len_bin / 4 + 1;
- }
-
- j = len_hex + 1;
-
- for(i = len_bin - 1;i >= 0;i --)
- {
- if(count < 4)
- {
- temp1 = (bin - '0') * temp + temp1;
- temp = temp * 2;
- count ++;
-
- if(i == 0)
- {
- hex[2] = temp1 + '0';
- }
- }
- else
- {
- if(temp1 >= 10)
- {
- hex[j] = temp1 - 10 + 'a';
- }
- else
- {
- hex[j] = temp1 + '0';
- }
-
- count = 0;
- temp = 1;
- temp1 = 0;
- i ++;
- j --;
- }
- }
-
- hex[len_hex + 2] = '\0';
-
- return hex;
- }
-
- int main()
- {
- int len_bin;
- char bin[MAX_SIZE];
-
- printf("Please input the binary:\n");
- scanf("%s",bin);
-
- len_bin = strlen(bin);
-
- charchar * hex = bin2hex(bin,len_bin);
-
- printf("The hex of bin is:%s.\n",hex);
-
- return 0;
- }
调试结果为:[objc] view plain copy
- Please input the binary:
- 1110100101011
- The hex of bin is:0x1d2b.
二进制转换成十六进制数跟二进制数转换成八进制数相同,也是有两种方法:一种是上面程序的方法,即将二进制数直接转换成十六进制数,另一个就是先将二进制数转换成十进制数,再将十进制数转换成十六进制数。another:
[objc] view plain copy
- #include <stdio.h>
- #include <string.h>
-
- #define MAX_SIZE 1024
-
- charchar * bin2hex(charchar * bin,int len_bin)
- {
- int i;
- int len_hex;
-
- int ten = 0;
- int temp = 1;
- int temp1 = 1;
- int step = -1;
-
- static char hex[MAX_SIZE];
-
- while(*bin != '\0')
- {
- ten = (*(bin + len_bin - 1) - '0') * temp + ten;
-
- temp = temp * 2;
- bin ++;
- len_bin = len_bin - 2;
- }
-
- while(ten - temp1 >= 0)
- {
- temp1 = temp1 * 16;
- step ++;
- }
-
- temp1 = temp1 / 16;
-
- len_hex = step + 1;
-
- hex[0] = '0';
- hex[1] = 'x';
-
- for(i = 2;i <= len_hex + 1;i ++)
- {
- if(ten / temp1 >= 10)
- {
- hex = (ten / temp1) - 10 + 'a';
- }
- else
- {
- hex = (ten /temp1) + '0';
- }
-
- ten = ten - (ten / temp1) * temp1;
- temp1 = temp1 / 16;
- }
-
- hex = '\0';
-
- return hex;
- }
-
- int main()
- {
- int len_bin;
- char bin[MAX_SIZE];
-
- printf("Please input the binary:\n");
- scanf("%s",bin);
-
- len_bin = strlen(bin);
- charchar * hex = bin2hex(bin,len_bin);
-
- printf("The hex of the binary is:%s.\n",hex);
-
- return 0;
-
- }
调试结果为:
[objc] view plain copy
- Please input the binary:
- 11110101010101011101101
- The hex of the binary is:0x7aaaed.
[url=] [/url]
|