#include <stdio.h>
 main()
{
 int number;
 int ch;
 char sh[100];
 int i=0,j=0;
 
 printf("input No in Hex: ");
 scanf("%s",&sh);
  number = 0;
 i =strlen(sh);
 j=0;
 while(i!=0)
 {
  ch = sh[j];
  printf("Ch:%d\t%d\t%d\n",ch,'7',ch-'7');
 
  if(('0' <= ch && ch <= '9'))
  {
   number = number * 16;
   printf("Number :%d\n",number);
   number = number + (ch - '0');
   }
 
  if(('A' <= ch && ch && ch <= 'Z'))
  {
   number = number * 16;
   printf("Number :%d\n",number);
   number = number + (ch - '7');
  }
 
  printf("Number :%d\n",number);
  i--;
  j++;
  
 }

 
 printf("The O/p No in Decimal is : %d\n",number);

 
}

Posted by : sheik@coromandel

Recommended Answers

All 4 Replies

It would be easier, and probably better, to use strtol, strtoul, or even sscanf, if you ask me.

Did you have a question about the code?

#include<stdio.h>
int main()
{
 char sh[100] = "ED",*stop;
 int test;
  
 test = strtol(sh,&stop,16);
  printf("Test:%d",test);
}
#include<stdio.h>
int main()
{
 char sh[100] = "ED",*stop;
 int test;
  
 test = strtol(sh,&stop,16);
  printf("Test:%d",test);
}

Very nice. I assume it works now...

why not just use:

printf("Input hexadecimal number: ");
int hex;
scanf("%x",hex);

Seems easiest to me, then it gets stored as an int. you can display it as a hex or a decimal: printf("%X",hex) Hex printf("%d",hex) Decimal

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.