Hexadecimal to decimal-C

Source: Internet
Author: User

We often encounter a situation where the hexadecimal number is converted to the hexadecimal number. We can use the following C program to complete the above work.

How does it work?

6.2.5 convert the hexadecimal number to the decimal number. The hexadecimal value is 16 to 1, but we only have 0 ~ 9. Therefore, we use the letters A, B, C, D, E, and F to represent 10, 11, 12, 13, 14, and 15 respectively. Uppercase letters are not case sensitive. The 0th bits in hexadecimal notation are 0 to the power of 16, 1st bits are 1 to the power of 16, and 2nd bits are 2 to the power of 16 ...... Therefore, in the Nth (N starts from 0) bit, if it is a number X (X is greater than or equal to 0, and X is less than or equal to 15, that is, F) it indicates the N power of X * 16. Suppose there is a hexadecimal number 2AF5, how can we convert it into a hexadecimal number? Convert 2AF5 to 10 hexadecimal: 0th bits: 5*160 = 5 1st bits: F * 161 = 240 2nd bits: A * 162 = 2560 3rd bits: 2*163 = 8192 + ----------------------------------- 10997 direct calculation is: (5*160) + (F * 161) + (A * 162) + (2*163) = 10997 (don't forget, in the above calculation, A indicates 10, and F indicates 15)
Note *


In this way, the program requires two components:

1. Weight Calculation

Power (16, x)

unsigned long power(int a, int b){  int i;  unsigned long result = 1;  for(i = 0; i < b; i++)  {    result *= a;  }  return result;};

2. convert a single hexadecimal value to a decimal Value

For example, if it is F, it indicates 15

   switch (ch)    {  case '0':iCh = 0;break;  case '1':iCh = 1;break;  case '2':iCh = 2;break;  case '3':iCh = 3;break;  case '4':iCh = 4;break;  case '5':iCh = 5;break;  case '6':iCh = 6;break;  case '7':iCh = 7;break;  case '8':iCh = 8;break;  case '9':iCh = 9;break;  case 'a':iCh = 10;break;  case 'b':   iCh = 11;break;  case 'c':  iCh = 12;break;  case 'd':  iCh = 13;break;  case 'e':  iCh = 14;break;  case 'f':  iCh = 15;break;  default:iCh = -1;break;  }

To meet the requirements, you can use the system function tolower () or our own function toLower () to convert all input to lowercase letters ()

int toLower(int c){  if(c >= 'A' && c <= 'Z')  {     return c + 'a' - 'A';  } else {     return c;  }};

The complete procedure is as follows:

/* * hex2int.c * *  Created on: 2010-07-20 *      Author: xiaobin * */#include 
 
  /* #include 
  
    */#include 
   
    //#include 
    
     /* int max is 32767 *//* "%d" only print int *//* long max is 2147483647 *//* "%ld" can print long type *//* unsigned long max is 4294967295 *//* "%lu" can print unsigned long type */#define MB 0x0100000ULunsigned long power(int a, int b);int toLower(int c);unsigned long htoi(char s[]);int main(int argc, char* argv[]){    if(argc > 1)        printf("long integer: %lu\n", htoi(argv[1]));    printf("M/Byte: %ld\n", MB);    return 0;}unsigned long htoi(char *s){    int i, len;    unsigned long value, result;    int iCh;    unsigned long iPow;    int pos;    char ch;    result = 0;    len = 0;    value = 0;    iCh = -1;    len = strlen(s);    pos = 0;    len -= 1;    for(i = len; (s[i] >= '0' && s[i] <= '9')                    || (s[i] >= 'a' && s[i] <= 'f')                    || (s[i] >= 'A' && s[i] <= 'F'); i--) {        ch = toLower(s[i]); /* tolower() defined in ctype.h */        switch (ch) {          case '0':            iCh = 0;            break;          case '1':            iCh = 1;            break;          case '2':            iCh = 2;            break;          case '3':            iCh = 3;            break;          case '4':            iCh = 4;            break;          case '5':            iCh = 5;            break;          case '6':            iCh = 6;            break;          case '7':            iCh = 7;            break;          case '8':            iCh = 8;            break;          case '9':            iCh = 9;            break;          case 'a':            iCh = 10;            break;          case 'b':           iCh = 11;            break;          case 'c':            iCh = 12;            break;          case 'd':            iCh = 13;            break;          case 'e':            iCh = 14;            break;          case 'f':            iCh = 15;            break;          default:            iCh = -1;            break;        }        iPow = power(16, pos);        pos++;        value = iPow * iCh;        result += value;    }    return result;};unsigned long power(int a, int b){    int i;    unsigned long result = 1;    for(i = 0; i < b; i++)        result *= a;            return result;};int toLower(int c){    if(c >= 'A' && c <= 'Z')        return c + 'a' - 'A';    else        return c;};
    
   
  
 


Compiled in CDT.


If you have any questions, please refer to: Using CDT + cygwin to write C/C ++

Note *: Refer to Chapter 6 binary, octal, and hexadecimal system-2nd xuanxiang Nanyu


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.