十六进制字符串转换为十进制数

非格式转换算法C语言版本

//十六进制字符串转换为十进制数 

#include "stdio.h"
#include "stdlib.h"
main()
{
   char a[8];
   int i=0;
   unsigned long nResult=0;   
   
   for(i=0;i<8;i++)
      a[i]='\0';                
      
   
   for(i=0;i<8;i++)
   {
     scanf("%c",&a[i]);
     if(a[i]=='\n')
        break;                
   }
   
    for(i = 0; i < 8; i++)
    {
        if(a[i] == '\0'||a[i]=='\n')
            break;
        
         
        nResult *= 16;//关键算法
        
        
        //分类处理 
        if(a[i] >= '0' && a[i] <= '9')
        {
            nResult += (a[i] - '0');
        }
        else if(a[i] >= 'A' && a[i] <= 'F')
        {
            nResult += (a[i] - 'A' + 10);
        }
        else if(a[i] >= 'a' && a[i] <= 'f')
        {
            nResult += (a[i] - 'a' + 10);
        }
       
    }
    
    printf("%ld", nResult);
    system("pause");

}

格式转换算法C语言版本

#include "stdio.h"
#include "stdlib.h"
main()
{
  int s=4;
  scanf("%x",&s);
  printf("%d",s);
  system("pause");      
}







你可能感兴趣的:(十六进制)