IP地址转换-16进制转化为10进制

来源:互联网 发布:php源码加密 编辑:程序博客网 时间:2024/04/26 07:39

很久之前写的,今天放在这里。

1、不含字母

[cpp] view plaincopyprint?
  1. #include <iostream>   
  2. #include <stdio.h>   
  3.   
  4. //8个二进制(2个十六进制)数转换成十进制数,不含a-f。即00-99的转换   
  5. int transfer_0(int x)  
  6. {  
  7.     int y;//结果  
  8.     int temp;//临时值  
  9.   
  10.     y=x%10;//个位数   
  11.     temp=(x%100-y)/10;//十位数   
  12.     y+=temp*16;  
  13.     return y;  
  14. }  
  15.   
  16. //32个二进制(4个十六进制)数转换成十进制数,不含a-f。即0000-9999的转换  
  17. void transfer_1(unsigned int x)  
  18. {  
  19.     //从右向左   
  20.     int temp1=x%100;  
  21.     int y1=transfer_0(temp1);  
  22.   
  23.     int temp2=(x%10000-temp1)/100;  
  24.     int y2=transfer_0(temp2);  
  25.   
  26.     int temp3=(x%1000000-temp1-temp2*100)/10000;  
  27.     int y3=transfer_0(temp3);  
  28.   
  29.     int temp4=(x%100000000-temp1-temp2*100-temp3*10000)/1000000;  
  30.     int y4=transfer_0(temp4);  
  31.   
  32.     printf("结果是:%d.%d.%d.%d\n",y4,y3,y2,y1);  
  33. }  
  34.   
  35. void main()  
  36. {  
  37.     unsigned int x;  
  38.     printf("IP地址:");  
  39.     scanf("%d",&x);  
  40.     transfer_1(x);  
  41.     system("pause");  
  42. }  

2、含有字母

[cpp] view plaincopyprint?
  1. #include <stdio.h>   
  2. #include <iostream>   
  3. #include <string>   
  4. using namespace std;  
  5.   
  6. //将16进制数数转化成10进制数,一位的0-F。   
  7. int transfer_0(char x)  
  8. {  
  9.     int y=0;//返回值  
  10.     if (x>='0' && x<='9')//0-9的数字  
  11.     {  
  12.         y=x-'0';  
  13.         return y;  
  14.     }  
  15.     if (x>='a' && x<='f')//a-f的字母  
  16.     {  
  17.         y=x-'a'+10;  
  18.         return y;  
  19.     }  
  20.     if (x>='A' && x<='F')//A-F的字母  
  21.     {  
  22.         y=x-'A'+10;  
  23.         return y;  
  24.     }  
  25.     printf("参数错误!");  
  26.     exit(1);  
  27. }  
  28.   
  29. /* 
  30. //将16进制数数转化成10进制数,两位的00-FF。 
  31. int transfer_1(char x[],int n)//长度为2 
  32. { 
  33.     char s[2];//固定的,长度不变 
  34.     for (int i=0;i<n;i++) 
  35.     { 
  36.         s[i]=x[i]; 
  37.     } 
  38.  
  39.     int y1=transfer_0(s[0]);//十位 
  40.     int y2=transfer_0(s[1]);//个位 
  41.     int y=y2+y1*16; 
  42.     return y;    
  43. } 
  44. */  
  45.   
  46. //尝试一次,尝试删除上面的,哈哈~~   
  47. //将16进制数数转化成10进制数,八位的00000000-FFFFFFFF。  
  48. void transfer_2(char x[],int n)//长度为8  
  49. {  
  50.     //从左往右   
  51.     int y0=transfer_0(x[0]);  
  52.     int y1=transfer_0(x[1]);  
  53.     int y2=transfer_0(x[2]);  
  54.     int y3=transfer_0(x[3]);  
  55.     int y4=transfer_0(x[4]);  
  56.     int y5=transfer_0(x[5]);  
  57.     int y6=transfer_0(x[6]);  
  58.     int y7=transfer_0(x[7]);  
  59.   
  60.     printf("结果是:%d.%d.%d.%d\n",y0*16+y1,y2*16+y3,y4*16+y5,y6*16+y7);      
  61. }  
  62.   
  63. void main()  
  64. {  
  65.     char x[9]="";     
  66.     printf("8位的IP地址(十六进制):");//不能有空位  
  67.     cin>>x;  
  68.     transfer_2(x,8);  
  69.     system("pause");  
  70. }  

3、提交版

[cpp] view plaincopyprint?
  1. #include <stdio.h>   
  2. #include <string>   
  3.   
  4. //将16进制数数转化成10进制数,一位的0-F。   
  5. int transfer_0(char x)  
  6. {  
  7.     int y=0;//返回值  
  8.     if (x>='0' && x<='9')//0-9的数字  
  9.     {  
  10.         y=x-'0';  
  11.         return y;  
  12.     }  
  13.     if (x>='a' && x<='f')//a-f的字母  
  14.     {  
  15.         y=x-'a'+10;  
  16.         return y;  
  17.     }  
  18.     if (x>='A' && x<='F')//A-F的字母  
  19.     {  
  20.         y=x-'A'+10;  
  21.         return y;  
  22.     }  
  23.     printf("参数错误!");  
  24.     exit(1);  
  25. }  
  26.   
  27. //将16进制数数转化成10进制数,八位的00000000-FFFFFFFF。  
  28. void transfer_2(char x[])  
  29. {  
  30.     //从左往右   
  31.     int y1=transfer_0(x[0])*16+transfer_0(x[1]);  
  32.     int y2=transfer_0(x[2])*16+transfer_0(x[3]);  
  33.     int y3=transfer_0(x[4])*16+transfer_0(x[5]);  
  34.     int y4=transfer_0(x[6])*16+transfer_0(x[7]);  
  35.   
  36.     printf("结果是:%d.%d.%d.%d\n",y1,y2,y3,y4);      
  37. }  
  38.   
  39. void main()  
  40. {  
  41.     char x[9]="";//长度为8   
  42.     printf("8位的IP地址(十六进制):");//不能有空位,只取前8位  
  43.     scanf("%s",&x);  
  44.     transfer_2(x);  
  45. }  

原创粉丝点击