Thread: hex to string problems

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    2

    hex to string problems

    I'v got this got to work for binary and deciaml by change it to a 2 or a 10 for the base, but when I use 16 for Hex, it doesn't produce the correct results for some reason. Any ideas?


    Code:
    if(base =='h')
    {
            int temp = x, quotient, remainder;
            int count = 0;
            int i;
            int p;
            int q;
    
            do{
                    quotient = temp/16;  //base
                    temp = quotient;
                    count++;
            }
            while(quotient != 0);
    
            count++;
            temp = x;
            //char *aray = new char[count];
    
            for(p=0, q=count-2; p<count-1; p++, q--)
            {
                    quotient = temp / power(16, q); //base
                    remainder = temp % (int)power(16, q);  //base
                    string[p] =  quotient+48;
                    temp = remainder;
            }
            string[p] = 0;//giving null character to last element of aray.
            return x;
    }

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by ERIKh22a
    I'v got this got to work for binary and deciaml by change it to a 2 or a 10 for the base, but when I use 16 for Hex, it doesn't produce the correct results for some reason. Any ideas?
    Do you know why this works for bin and dec ascii digits?
    Code:
       string[p] =  quotient+48;
    ASCII '0' = decimal 48
    ASCII '1' = decimal 49


    etc.

    but if you want ascii chars for 'a' - 'f' or 'A' - 'F', you have to do a little more work (not much)

    Perhaps you can make a function that returns the ASCII char for all integers from 0 - 15. Call it, say, decimal_hex_value_to_ascii_char(), or something. Then do this:

    Code:
       string[p] =  decimal_hex_value_to_ascii_char(quotient);
    Regards,

    Dave

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Dave Evans
    Do you know why this works for bin and dec ascii digits?
    Code:
       string[p] =  quotient+48;
    ASCII '0' = decimal 48
    ASCII '1' = decimal 49


    etc.
    Of course, that's non-portable.

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

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    2
    Quote Originally Posted by Dave Evans
    Do you know why this works for bin and dec ascii digits?
    Code:
       string[p] =  quotient+48;
    ASCII '0' = decimal 48
    ASCII '1' = decimal 49


    etc.

    but if you want ascii chars for 'a' - 'f' or 'A' - 'F', you have to do a little more work (not much)

    Perhaps you can make a function that returns the ASCII char for all integers from 0 - 15. Call it, say, decimal_hex_value_to_ascii_char(), or something. Then do this:

    Code:
       string[p] =  decimal_hex_value_to_ascii_char(quotient);
    Regards,

    Dave
    Something liek this
    Code:
    string[p] =  hexa(quotient);
    
    
    
    
    int hexa( char hex )
    
    
            {
            switch(hex ) { 
            case 'F': 
            case 'f': return( 15);
            case 'E':
            case 'e': return( 14);
            case 'D':
            case 'd': return( 13);
            case 'C':
            case 'c': return( 12);
            case 'B':
            case 'b': return( 11);
            case 'A':
            case 'a': return( 10);
            case '9': return( 9);
            case '8': return( 8);
            case '7': return( 7);
            case '6': return( 6);
            case '5': return( 5);
            case '4': return( 4);
            case '3': return( 3);
            case '2': return( 2);
            case '1': return( 1);
            default: return( 0);
            	}
    Last edited by ERIKh22a; 03-02-2005 at 09:17 PM.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Or how about...
    Code:
    int hex_to_int(char hexnum)
    {
      char array[] = "0123456789abcdef";
      char *ptr;
    
      if((ptr = strchr(array, tolower(hexnum))))
        return ptr - array;
    
      return 0;
    }
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    Quote Originally Posted by itsme86
    Or how about...
    Code:
    int hex_to_int(char hexnum)
    {
      char array[] = "0123456789abcdef";
      char *ptr;
    
      if((ptr = strchr(array, tolower(hexnum))))
        return ptr - array;
    
      return 0;
    }

    Any other ideas? I couldn't get it to implement..

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    What problem did you have implementing it? Looks pretty simple to me.

    As an aside though, I would have the function return -1 on error instead of 0. Otherwise there isn't any way to distinguish between an error or if "0" is passed.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    I can only get it to print out numbers 10-15


    Quote Originally Posted by bithub
    What problem did you have implementing it? Looks pretty simple to me.

    As an aside though, I would have the function return -1 on error instead of 0. Otherwise there isn't any way to distinguish between an error or if "0" is passed.
    Code:
    if(base =='h')
    {
       int temp = x, quotient, remainder;
       int count = 0;
       int i;
       int p;
       int q;
    
       do
          {
          quotient = temp/16;
          temp = quotient;
          count++;
          }while(quotient != 0);
    
       count++;
       temp = x;
    
       for(p=0, q=count-2; p<count-1; p++, q--)
          {
          quotient = temp / power(16, q);
          remainder = temp % (int)power(16, q);
          string[p] =  hexa(quotient);
          temp = remainder;
          }
    
       string[p] = 0;
    
       return x;
    }
    
    int hexa(char hex)
    {
            if(hex <10)
            return hex;
    else
    
             if (hex == 10)
            return 'A';
            if (hex == 11)
            return 'B';
            if (hex == 12)
            return  'C';
            if (hex == 13)
            return 'D';
            if (hex == 14)
            return  'E';
            if (hex == 15)
            return 'F';
    
    return 0;
    }

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    Nevermind got it to work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. io stream and string class problems
    By quizteamer in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2005, 12:07 AM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM