Thread: beginner help

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    45

    beginner help

    Hi, I've tried and tried to make my own hex to decimal (myhtoi) function but am not succeeding
    here's what I've got so far:
    Code:
    // a program to convert hex code into its equivalent decimal value
    
    // myhtoi.c
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    
    int myhtoi(char string[]);          // prototype of function (myhtoi)
    
    int main(void)
    {
    	char hex[] = "0x00000110";         // declare string of hex digits
    	myhtoi(hex);                       // call the function (myhtoi)
    	return 0;                          // exit program
    }
    
    int myhtoi(char string[])        // now make the function (myhtoi)
    {
    	int i, n;           // used for counting and holding the value later
    	n = 0;              // set the default value of n
    	
    	for (i = 2; string[i] >= '0' && string[i] <= '9'; i++)       // loop until condition is met
    	{
    		
    	}
    	
    	printf("That hex code in decimal is %d\n", n);
    	return 0;
    }
    I've tried:
    Code:
    n = 16 * n + (string[i] - '0');
    but it only works for 1 digit only.
    can anyone help me?
    Thanks.
    Last edited by cprog12; 05-12-2011 at 02:01 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why do you claim that it only works for 1 digit only? And what happens when you try to convert "0xA"?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    45
    because i've already tried it, it works for 0x00000010 and results in 16 but when i try 0x00000110 which should result in 48
    it gives a very wierd result.
    and i want to get 0-9 out of the way first before i want to deal with a-f.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I think he means that: 0x11 the first gets converted, and promptly discarded when he tries to convert the next.

    He needs to be combining the previous number with the next. Like if I had a string "11" and I wanted to convert it to a decimal, I convert the first (right most) 1 to an actual 1, then I need to do the next one, and add that ten to the first one to get eleven.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    0x00000110 is surely not 48! 0x000000030 is 48. 0x00000110 is 256+16 = 272.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    45
    lol you're right i'm abit rusty i've been doin 16 * 2 forgot it was 16 to the power of 2 sorry, i will try my code again

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    45
    ha it works sorry for wasting your time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beginner help
    By jycottonmouth in forum C++ Programming
    Replies: 4
    Last Post: 01-04-2007, 11:57 PM
  2. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM
  3. please help a beginner
    By karen66 in forum C Programming
    Replies: 5
    Last Post: 12-16-2002, 07:02 AM
  4. C++ Beginner Needs Help!!!
    By DeanDemon in forum C++ Programming
    Replies: 2
    Last Post: 11-29-2002, 09:23 AM
  5. help this beginner!!
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 05-27-2002, 11:37 PM