Results 1 to 10 of 10

Thread: Class or Template? Convert integers into decimal, octal, and hexadecimal.

  1. #1

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    Class or Template? Convert integers into decimal, octal, and hexadecimal.

    Ok I started writting this as a class, then realized a template may be better.

    What I need to do is take 3 numbers (10, 010, 0x10 for example) decimal, octal, and hexadecimal and then output the number in all 3 formats. If 0x10 was input then I would need to output that number in decimal, octal, and hexadecimal format.

    Now I figure a template would be the best way to go since 3 different kinds of numbers will be getting input into the same function. But I am not sure how to handle the conversion of these numbers. I think there is something in the stream manipulation that can handle this, but I would appreciate some help figuring it out.

    Also, I am not sure, but I think I would need to return an array from the function that held all three conversions of the number. I guess it would have to be a string array unless 0x10 (for example) can be held in a int array. This is also something I do not know how to do..I can do it in VB but not C++.

    Any help is appreciated!

    PHP Code:
    template<class T>
    string convertInt( const )
    {

    //convert numbers to dec, hex and oct values and return all 3


    }//end convertInt 
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  2. #2
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771

    Re: Class or Template? Convert integers into decimal, octal, and hexadecimal.

    This is not somewhere you should use a template, since the numbers are always converted from strings to integers or the other way around.
    In this case the easiest way would be to use the C sprintf/sscang functions, because they support the octal and decimal format:
    Code:
    #include <cstdio>
    #include <string>
    
    std::string numberInThreeFormats(const std::string& str) {
        char buffer[255]; // large enough to hold any number three times
        int number;
        std::sscanf(str.c_str(), "%i", number); // %i = integer in decimal, octal or hexadecimal
        std::sprintf(buffer, "%d, 0%o, 0x%x", number, number, number); // %d = decimal, %o = octal, %x = hexadecimal
        return buffer;
    }
    All this is also possible using C++ iostreams for example, but you would have to do more work yourself.

  3. #3

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    Re: Class or Template? Convert integers into decimal, octal, and hexadecimal.

    Yah I have to use Iostream in C++ . But thanks for the post.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  4. #4

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    Re: Class or Template? Convert integers into decimal, octal, and hexadecimal.

    So does anyone know how to tell if a number is a decimal, hex or oct using iostream?

    I need to input a number and tell if its a decimal, hex or oct and if so return what it is...if it isn't then return an error.

    That is easy enough if I can figure out how to tell if the input is any of those or not.


    Thanks!
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  5. #5
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771

    Re: Class or Template? Convert integers into decimal, octal, and hexadecimal.

    You could first read it into a string, and then check if it starts with "0x" or "0".

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Class or Template? Convert integers into decimal, octal, and hexadecimal.

    cout << oct << number << hex << number << dec << number;

    This is much work, how?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    Re: Class or Template? Convert integers into decimal, octal, and hexadecimal.

    Well you see that only converts integers or doubles to into hex dec and oct.

    I need to able to accept a hex dec or oct as input and then convert it to all three.

    Since a hex (i.e. 0x10) cant be input as an integer or a double then it has to be a string. and those iostream functions do not work on strings.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Re: Class or Template? Convert integers into decimal, octal, and hexadecimal.

    how about doing the base conversion manually?
    i=last,int b=1,a=0;
    while(i>first)a+=*i--*b*=e1;
    while(a)*j--=a%e2,a/=e2;

    where first and last points to the first and last element in the char array to be converted from base e1 to base e2 in the char array j.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Class or Template? Convert integers into decimal, octal, and hexadecimal.

    Why do something manually that you already have?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Re: Class or Template? Convert integers into decimal, octal, and hexadecimal.

    well depends, there are always special cases which can be optimised
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width