would you explain to me? hexadecimal to decimal

This is a discussion on would you explain to me? hexadecimal to decimal within the C Programming forums, part of the General Programming Boards category; The below coding can correctly print out the result. But i do not understand the last part. i--; for(;i>=0;i--) { ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    4

    would you explain to me? hexadecimal to decimal

    The below coding can correctly print out the result. But i do not understand the last part.
    i--;
    for(;i>=0;i--)
    {
    h=h+a[i]*m;
    m=m*16;
    }
    how the i-- and m functions?


    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    
    void hd();
    
    void main()
    {
    		
    		printf("Enter Hexadecimal to Decimal:\n");
    		
    		{
    		 int i,a[20];
    		 unsigned long int h=0,m=1;
    		 char s[20];
    		 
    		 printf("Convertion from Hexadecimal to Decimal\n");
    		 printf("Enter a Hexadecimal number\n");
    		 scanf("%s",s);
    		 printf("Decimal Equivalent=");
    		 for(i=0;s[i]!=NULL;i++)
    		 {
    		  switch(s[i])
    		   {
    		    case '0':
    		     a[i]=0;
    		     break;
    		    case '1':
    		     a[i]=1;
    		     break;
    		    case '2':
    		     a[i]=2;
    		     break;
    		    case '3':
    		     a[i]=3;
    		     break;
    		    case '4':
    		     a[i]=4;
    		     break;
    		    case '5':
    		     a[i]=5;
    		     break;
    		    case '6':
    		     a[i]=6;
    		     break;
    		    case '7':
    		     a[i]=7;
    		     break;
    		    case '8':
    		     a[i]=8;
    		     break;
    		    case '9':
    		     a[i]=9;
    		     break;
    		    case 'a':
    		    case 'A':
    		     a[i]=10;
    		     break;
    		    case 'b':
    		    case 'B':
    		     a[i]=11;
    		     break;
    		    case 'c':
    		    case 'C':
    		     a[i]=12;
    		     break;
    		    case 'd':
    		    case 'D':
    		     a[i]=13;
    		     break;
    		    case 'e':
    		    case 'E':
    		     a[i]=14;
    		     break;
    		    case 'f':
    		    case 'F':
    		     a[i]=15;
    		     break;
    		    
    		   }
    		 }
    		 i--;
    		 for(;i>=0;i--)
    		 {
    		  h=h+a[i]*m;
    		  m=m*16;
    		 }
    		  printf("%ld ",h);
    }
    }
    Last edited by shteo83; 02-25-2006 at 02:40 PM.

  2. #2
    Registered User
    Join Date
    Dec 2003
    Posts
    167
    The last part of the code multiples the value of each digit by an increasing power of 16.

    For example if you enter 0xA25. The final loops calculates.

    10*(16*16) + 2*16 + 5 * 1 = 2560 + 32 + 5 = 2597
    silk.odyssey

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    4
    Oh...i see... suddenly realize by your solution.. thank you very much. Nice to see you here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. decimal to hexadecimal
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 11-02-2008, 09:07 PM
  2. Decimal to hexadecimal conversion
    By Nathalie in forum C Programming
    Replies: 9
    Last Post: 12-11-2007, 03:29 PM
  3. stacks to convert from decimal to hexadecimal
    By drdodirty2002 in forum C++ Programming
    Replies: 3
    Last Post: 09-26-2004, 01:24 AM
  4. All u wanted to know about data types&more
    By SAMSAM in forum Windows Programming
    Replies: 6
    Last Post: 03-11-2003, 03:22 PM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 11:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21