Hex to Dec Problem

This is a discussion on Hex to Dec Problem within the C Programming forums, part of the General Programming Boards category; I tried to use writeStr to print out if the Hex converted to a decimal but all I get is ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    82

    Hex to Dec Problem

    I tried to use writeStr to print out if the Hex converted to a decimal but all I get is the input i passed thru strToInt.

    for example i chose A and it prints out an A but i wanted to print out a 10

    Code:
    #include <stdio.h>
    #include "readStr.h"
    #include "writeStr.h"
    #include "strToInt.h"
    
    #define BUFFER_SIZE 33
    #define HEX_SIZE 9
    #define BIN_SIZE 33
    #define DEC_SIZE 11
    #define BASE_SIZE 2
    
    int main()
    {
    
    char input[BUFFER_SIZE];
    char base[2];
    unsigned int x;
    
    writeStr("\nEnter the base of the value to be converted");
    writeStr("\n d-decimal, h-hexadecimal, b-binary: ");
    
    readStr(base, BASE_SIZE);
    
    if(base[0] =='d' || base[0] =='D')
    {
    writeStr("Enter the Decimal Value: ");
    readStr(input, 11);
    }
    
    else if(base[0] =='h' || base[0] =='H')
    {
    writeStr("Enter the Hex Value: ");
    readStr(input, 9);
    }
    else if(base[0] =='b' || base[0] =='B')
    {
    writeStr("Enter the Binary Value: ");
    readStr(input, 33);
    }
    else
    {writeStr("Invalid Input - Goodbye");}
    
    
    strToInt(input, base[0]);
    writeStr(input);
    
    return 0;
    }



    Code:
    int strToInt(char *string, int base)
    {
    int decimal_val;
        int result = 0;
        int i;
    
    if(base == 'h' || base =='H')
    {
    
        /* Goes through each value of string*/
        for ( i = 0 ; string[i] != '\0' ; i++ )
            {
            /* The conversion */
            decimal_val =
                '0' <= string[i] && string[i] <= '9' ?
                    string[i] - '0' :
                    'a' <= string[i] && string[i] <= 'f' ?
                        string[i]- 'a' + 10 :
                        'A' <= string[i] && string[i] <= 'F' ?
                             string[i]- 'A' + 10 :
                             0;
    
            result = result * 16 + decimal_val;
        }
    
    }
    return result;
    
    }

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by gqchynaboy
    I tried to use writeStr to print out if the Hex converted to a decimal but all I get is the input i passed thru strToInt.

    for example i chose A and it prints out an A but i wanted to print out a 10

    Code:
    int main()
    {
    
    strToInt(input, base[0]);
    writeStr(input);
    
    }
    strToInt() returns the integer value of the input string. You don't use this value; instead you merely print the input string. And you were expecting ???

    Regards,

    Dave

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    Quote Originally Posted by Dave Evans
    strToInt() returns the integer value of the input string. You don't use this value; instead you merely print the input string. And you were expecting ???

    Regards,

    Dave
    Code:
    int strToInt(char *string, int base)
    {
    int decimal_val;
        int result = 0;
        int i;
    
    if(base == 'h' || base =='H')
    {
    
        /* Goes through each value of string*/
        for ( i = 0 ; string[i] != '\0' ; i++ )
            {
            /* The conversion */
            decimal_val =
                '0' <= string[i] && string[i] <= '9' ?
                    string[i] - '0' :
                    'a' <= string[i] && string[i] <= 'f' ?
                        string[i]- 'a' + 10 :
                        'A' <= string[i] && string[i] <= 'F' ?
                             string[i]- 'A' + 10 :
                             0;
    
            result = result * 16 + decimal_val;
        }
    
    }
    return result;
    
    }
    I thought it will convert a Hex to a decimal and return the decimal integer..

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by gqchynaboy



    I thought it will convert a Hex to a decimal and return the decimal integer..
    It does. That is, it converts a string of characters representing hex digits to an integer value.

    So how do you expect to display the value? (What do you do with the integer value returned to main()?)

    Regards,

    Dave
    Last edited by Dave Evans; 03-01-2005 at 04:27 PM.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Code:
    unsigned int i;
    
    printf("Enter a base 10 positive integer: ");
    scanf("%u",&i);
    printf("The value of %u in base 16 is %x",i,i);
    
    ----------
    
    
    printf("Enter a base 16 positive integer :");
    scanf("%x",&i);
    printf("The value of %x in base 10 is %u",i,i);
    why do so much work when its all already in the standard library? Check the info on scanf and printf for your implementation though, but those are pretty standard IIRC.

    The question isn't retorical either, as I don't really know what you are doing, so this many not help.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    Quote Originally Posted by Xipher
    Code:
    unsigned int i;
    
    printf("Enter a base 10 positive integer: ");
    scanf("%u",&i);
    printf("The value of %u in base 16 is %x",i,i);
    
    ----------
    
    
    printf("Enter a base 16 positive integer :");
    scanf("%x",&i);
    printf("The value of %x in base 10 is %u",i,i);
    why do so much work when its all already in the standard library? Check the info on scanf and printf for your implementation though, but those are pretty standard IIRC.

    The question isn't retorical either, as I don't really know what you are doing, so this many not help.
    we can't use scanf and printf......that is why we have to convert it ourselfs

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Quote Originally Posted by gqchynaboy
    we can't use scanf and printf......that is why we have to convert it ourselfs
    Ok, thats why I was asking.

    After reading the other replies, the #1 problem I see, is you don't use your result at all from the coversion, your simply printing the input string. Now if you want to print the result, I suspect you will also need to convert the int value into a string, and print that.
    Last edited by Xipher; 03-01-2005 at 09:36 PM.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    Quote Originally Posted by Xipher
    Ok, thats why I was asking.

    After reading the other replies, the #1 problem I see, is you don't use your result at all from the coversion, your simply printing the input string. Now if you want to print the result, I suspect you will also need to convert the int value into a string, and print that.
    http://www.cs.sonoma.edu/~stauffer/2....Project1.html

    "intToStr.c - This function takes three arguments, an unsigned int, the address of a char array, and a char indicating the string base. The string base argument is a 'd' for decimal, 'h' for hexadecimal, and 'b' for binary."

    so the function woudl basically look something like this, then I would have to convert hex to string, dec to string, and binary to string? anywhere i can find some code/help

    intToStr(int x, char *string, char base)

    If i enter a hex how can i print it out to show a decimal? I call strToInt and convert it..but then I dont know where to go from there


    Code:
    int main()
    {
    
    char input[BUFFER_SIZE];
    char base[2];
    unsigned int x;
    
    writeStr("\nEnter the base of the value to be converted");
    writeStr("\n d-decimal, h-hexadecimal, b-binary: ");
    
    readStr(base, BASE_SIZE);
    
    if(base[0] =='d' || base[0] =='D')
       {
       writeStr("Enter the Decimal Value: ");
       readStr(input, 11);
       }
    
    else if(base[0] =='h' || base[0] =='H')
       {
       writeStr("Enter the Hex Value: ");
       readStr(input, 9);
       }
    
    else if(base[0] =='b' || base[0] =='B')
       {
       writeStr("Enter the Binary Value: ");
       readStr(input, 33);
       }
    
    else
       {writeStr("Invalid Input - Goodbye");}
    
    
    strToInt(input, base[0]);
    
    
    writeStr("Input in decimal =");
    
    writeStr(input);
    
    /*intToStr(x, input, 'd');*/
    return 0;
    }
    Last edited by gqchynaboy; 03-01-2005 at 10:43 PM.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I call strToInt and convert it..but then I dont know where to go from there
    *smashes head on desk*

    Are you even paying attention to what people are telling you? USE THE RETURN VALUE!

    Code:
    int x;
    ...
    
    x = strToInt( input, base[0] );
    printf("The returned value is %d!", x );
    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by gqchynaboy
    [url]
    "intToStr.c - This function takes three arguments, an unsigned int, the address of a char array, and a char indicating the string base. The string base argument is a 'd' for decimal, 'h' for hexadecimal, and 'b' for binary."
    1. Use readstr() to get the string in input buffer.
    OK; You've done that.

    2. Use the buffer that you used in readStr() as an argument to strToInt() to convert to an int of whatever base the input is supposed to be.
    OK; You've done that.

    3. Use the value returned from strToInt() as an argument of intToStr(). The other argument is the base.
    You haven't done that, in spite of gentle and not-so-gentle hints given to you.

    4. Use the string in the buffer that was an argument to intToStr() as an argument to writeStr() so that you can print the result of the conversion to whatever base the output is supposed to be (decimal).

    I think you have totally missed the point of step 3.

    From my original post:
    Quote Originally Posted by Dave Evans

    strToInt() returns the integer value of the input string. You don't use this value; instead you merely print the input string.
    From my other post (about strToInt()):
    Quote Originally Posted by Dave Evans
    it converts a string of characters representing hex digits to an integer value.

    So how do you expect to display the value? (What do you do with the integer value returned to main()?)
    Regards,

    Dave

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    Quote Originally Posted by quzah
    *smashes head on desk*

    Are you even paying attention to what people are telling you? USE THE RETURN VALUE!

    Code:
    int x;
    ...
    
    x = strToInt( input, base[0] );
    printf("The returned value is %d!", x );
    Quzah.
    I can't use printf, but writeStr so when i use writeStr("Decimal is %d", x); there are to many arguements of course

    Code:
    #include <unistd.h>
    #include "writeStr.h"
    
    int writeStr(char *theString)
    {
    unsigned int length =0;
    
    write(STDOUT_FILENO, theString,1);
    
    while(*theString !='\0')
       {
       theString++;
       length++;
    
       write(STDOUT_FILENO, theString,1);
       }
    
    
    return length;
    }

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,167
    Try to be a little imaginative:
    Code:
    {
      char buf[100];
    
      x = strToInt( input, base[0] );
      sprintf(buf, "The returned value is %d!", x );
      writeStr(buf);
    }
    If you understand what you're doing, you're not learning anything.

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    Quote Originally Posted by itsme86
    Try to be a little imaginative:
    Code:
    {
      char buf[100];
    
      x = strToInt( input, base[0] );
      sprintf(buf, "The returned value is %d!", x );
      writeStr(buf);
    }

    Where not allowed to use sprintf or any other libraries just writeStr.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well then I guess you'd better write yourself a function to change an integer into a string too, huh?

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

  15. #15
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by gqchynaboy
    Where not allowed to use sprintf or any other libraries just writeStr.
    from my previous post:
    Quote Originally Posted by Dave Evans
    Use the string in the buffer that was an argument to intToStr() as an argument to writeStr() so that you can print the result of the conversion to whatever base the output is supposed to be (decimal).
    So, if you called intToStr(buf, 'd'), for example, maybe you could do something like this:

    Code:
      writeStr("The converted value is ");
      writeStr(buf);
      writeStr("\n");
    Or am I just being dense?

    Regards,

    Dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 03-26-2008, 09:01 AM
  2. Please help hex bytes to word to dec...
    By stormystones in forum C Programming
    Replies: 6
    Last Post: 11-11-2005, 11:46 AM
  3. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM

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