-2
#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

Edited by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

4
Contributors
4
Replies
24
Views
7 Years
Discussion Span
Last Post by prushik
0

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?

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

Edited by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

0
#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...

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.