Nov 25, 2016

K & R : Exercise 2.3 - htoi - hex to integer

K & R : Exercise 2.3 - htoi - hex to integer

Problem

Write the function htoi(s) which converts a string of hexadecimal digits (including an optional 0x, 0X) into its equivalent integer value. Allowable digits are 0..9, a through f, A through F.


Solution

Lets write a function called htoi which does things that is explained in the problem.

#include <stdio.h>
#include <string.h>
int htoi(const char *s);
int main()
{
  char *s = "0x1Af";

  printf("integer value of hex %s is %d\n", s, htoi(s));

  return 0;
}
int htoi(const char *s)
{
   int i = 0;
   int res = 0;
   if(s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
      i = 2;
   }
   for(; i < strlen(s); i++) {
     res = res * 16;
      if(s[i] >= '0' && s[i] <= '9') {
         res = res + s[i] - '0';
      }else if(s[i] >= 'a' && s[i] <= 'f') {
         res = res + s[i] - 'a' + 10;
      }else if(s[i] >= 'A' && s[i] <= 'F') {
         res = res + s[i] - 'A' + 10;
      }
   }
   return res;
}
integer value of hex 0x1Af is 431

Links

1 comment :