Thread: trying to convert hexadecimal to decimal

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    6

    trying to convert hexadecimal to decimal

    I am using wmware player with centos and it keeps saying that there is something wrong with my pow method and i can't figure it out. When i run the program it doesn't give me any output. Oh and i'm going to try to add a code tag, i really hope i do this right, if not i'm sorry.
    Code:
    #include<stdio.h>
    #include<math.h>
    main()
    {
       char s[1000],c;
       int i,A,B,C,D,E,F,j;
       double k,g;
       i,j=0;
       g,k=0.0;
       A=10;
       B=11;
       C=12;
       D=13;
       E=14;
       F=15;
       while((c=getchar()) !='\n')
       {
         s[i++]=c;
       }
       s[i]='\0';
       for(j=i;j>=0;j--)
       {
         g=g+(((int)s[j])*(pow(16,k)));
         k++;
       }
       putchar(g);
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    What is g and i equal to when you start using them in your code?

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    Quote Originally Posted by Subsonics View Post
    What is g and i equal to when you start using them in your code?
    g is initialized at 0 and so is i but i is then goes through the while loop and becomes how many positions in the array and g goes through the for loop and becomes the answer.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by chrissy2860 View Post
    g is initialized at 0 and so is i but i is then goes through the while loop and becomes how many positions in the array and g goes through the for loop and becomes the answer.
    There is no line in your posted code that initialized those two variables.
    Note: "," is not an assignment operator.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    Quote Originally Posted by stahta01 View Post
    There is no line in your posted code that initialized those two variables.
    Note: "," is not an assignment operator.

    Tim S.
    oh yea the way to do that is i=j=0 and g=k=0.0 I forgot, thanks...i changed it but i still get this error:
    Code:
    /tmp/ccUFewuZ.o: In function `main':
    hw3.c:(.text+0xb7): undefined reference to `pow'
    collect2: ld returned 1 exit status

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by chrissy2860 View Post
    oh yea the way to do that is i=j=0 and g=k=0.0 I forgot, thanks...i changed it but i still get this error:
    Code:
    /tmp/ccUFewuZ.o: In function `main':
    hw3.c:(.text+0xb7): undefined reference to `pow'
    collect2: ld returned 1 exit status
    Link in the math library; normally called just the letter "m".

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    Now it actually compiles but it doesn't give me any output when i put in a hexadecimal number, why doesn't it work...i did the gcc -lm hw3.c -o hw3 and ./hw3 -lm, is there anything else wrong with the code?

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by chrissy2860 View Post
    Now it actually compiles but it doesn't give me any output when i put in a hexadecimal number, why doesn't it work...i did the gcc -lm hw3.c -o hw3 and ./hw3 -lm, is there anything else wrong with the code?
    You print g with putchar, it's for characters so it will print a corresponding ascii value for g. g might be way larger than 127 since you raise 16 to the power of k in each iteration of your loop.

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. would you explain to me? hexadecimal to decimal
    By shteo83 in forum C Programming
    Replies: 2
    Last Post: 02-25-2006, 03:55 PM
  4. stacks to convert from decimal to hexadecimal
    By drdodirty2002 in forum C++ Programming
    Replies: 3
    Last Post: 09-26-2004, 12:24 AM
  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