CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 4 1234 LastLast
Results 1 to 15 of 49
  1. #1
    Join Date
    May 2003
    Posts
    424

    itoa replacement ALMOST perfect

    This code is almost perfect. The only problem occurs (commented in code below). The problem does not occur if T is an int and n is int_min. Little help please. There is a solution to this because ostringstream gets it right.
    PHP Code:
    #define int64_max 9223372036854775807LL
    #define int64_min (-int64_max-1)

    template <typename T>
    aStr IntToStr(T nuint radix10)
    {
        if(!
    n)
        {
            return 
    "0";
        }
        else
        {
            
    bool neg0;
            if(
    neg)
            {
                
    n= -n;
    MB(n); //when T is __int64 and n is int64_min, n stays as int64_min!
            
    }
            const 
    uint bitssizeof(T)*8;
            
    char buf[bits];
            
    // Set chars from the end of the buffer
            
    charptr= &buf[bits-1];
            *
    ptr0//terminating NULL
            
    while(0)
            {
                --
    ptr;
                
    //*ptr= itoaChars[n%radix]; //this works too
                
    *ptr=  n%radix+'0';
                
    /= radix;
            }
            if(
    neg)
            {
                --
    ptr;
                *
    ptr'-';
            }
            return 
    aStr(ptr);
        }


  2. #2
    Join Date
    Jun 2005
    Posts
    67

    Re: itoa replacement ALMOST perfect

    please show the error message

  3. #3
    Join Date
    May 2003
    Posts
    424

    Re: itoa replacement ALMOST perfect

    There is no error, it just outputs jibberish such as: x*((u)60($

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: itoa replacement ALMOST perfect

    Quote Originally Posted by aewarnick
    This code is almost perfect.
    No it isn't. See comments below.
    The only problem occurs (commented in code below). The problem does not occur if T is an int and n is int_min.
    You should be using the constants defined in <limits.h> and not hard-code minimum and maximum values. Not everyone has a 64-bit machine to test your code on.

    What is wrong with just using itoa? Is it for the same (possibly wrong) reason you wanted to code your own gcvt from this thread?

    http://www.codeguru.com/forum/showthread.php?t=357633

    Quite honestly, I don't look too seriously on code that is supposed to replace standard library functions. Usually the code that is supposed to "replace" the standard library functions is inferior to the standard library code in maintainability, speed, and a whole host of other areas.

    Right now, you have a bug, so already you are going uphill when you don't need to. I already pointed out that your code is only for 64-bits. What if you want to take this code to a 32-bit machine? So in reality, you haven't replaced itoa, you are trying to write a limited subset of itoa.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 20th, 2005 at 11:18 PM.

  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: itoa replacement ALMOST perfect

    given that this is C++ (you've used a template) why not use standard libraries?
    What is aStr? What is MB?

  6. #6
    Join Date
    May 2003
    Posts
    424

    Re: itoa replacement ALMOST perfect

    One of the reasons I'm making this code is that itoa is inferior. It only accepts int.

    Now that my motive is clear...I am using a 32 bit machine, I don't see where you are getting the idea that the code only works for 64 bit. The code does work perfectly for int32_max, uint32_max, uint64_max, int64_max...all the time. It's only when n is at a negative max value.

    When n is -2147483648 and I do this:
    n= -n;
    the max of int is 2147483647, 2147483648 is too big, so it just remains the same; -2147483648. And that is where the problem is.
    Last edited by aewarnick; September 21st, 2005 at 09:54 AM.

  7. #7
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: itoa replacement ALMOST perfect

    You can always use a stringstream:
    Code:
    std::stringstream ss;
    ss << my_int;
    Just like Paul McKenzie said, I'm a bit reluctant when it comes to rewrite standard functions...
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  8. #8
    Join Date
    May 2003
    Posts
    424

    Re: itoa replacement ALMOST perfect

    What if I want to use this in a C not C++ library sometime in the future?

    Lets look at this from a different angle - You make a convert to absolute value function that works for any integer type; int, int64, int128...

    PHP Code:
    template<typename T>
    T Abs(T t)
    {
        return -
    t;
    }
    MB(Abs(int_min)); //output -2147483648    WRONG!
    MB(Abs(int_min+1)); //output 2147483647 
    How would you solve that? Keep in mind that you can't use abs because that function does not accept __int64.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: itoa replacement ALMOST perfect

    Quote Originally Posted by aewarnick
    What if I want to use this in a C not C++ library sometime in the future?
    1) This is a C++ forum.

    2) Since you posted a template, it is assumed you are using this in C++, not C. Therefore you will get C++ answers. Next time state up front you are writing this for a C compiler. No one can read minds.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: itoa replacement ALMOST perfect

    Quote Originally Posted by aewarnick
    One of the reasons I'm making this code is that itoa is inferior. It only accepts int.

    Now that my motive is clear...I am using a 32 bit machine, I don't see where you are getting the idea that the code only works for 64 bit.
    Code:
    #define int64_max 9223372036854775807LL
    This will not compile on a compiler where the maximum int is 32 bits. In other words, your code is highly non-portable.

    You never write code assuming these types of limits. You always use the constants defined in limits.h to determine what is the maximum integer supported by the compiler.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    Re: itoa replacement ALMOST perfect

    Quote Originally Posted by aewarnick
    One of the reasons I'm making this code is that itoa is inferior. It only accepts int.
    Just to add,

    The itoa() is a 'C' function that cannot be overloaded. Therefore it can only have one type, and the type that is chosen is int. An "int" is the standard integer type for C++ where the sizeof(int) is compiler dependent. There is no such standard type as int64, __int64, or anything like that. Yes, you've used these types, but all of those are compiler creations and not officially part of the ANSI standard.

    So saying that itoa() is inferior is not a fair statement. If your compiler library vendor were smart, there would be functions called itoa_64() or abs_64() or something similar to that if the compiler has 64 bit numbers. I would be highly surprised if the compiler didn't have these functions, given that the compiler has native 64-bit integers.

    Regards,

    Paul McKenzie

  12. #12
    Join Date
    Feb 2003
    Posts
    377

    Re: itoa replacement ALMOST perfect

    Just wanted to mention that itoa is not standard. Writing a version of it that uses only standard code and is portable is not a bad idea, although I personally just prefer to use a stringstream.

  13. #13
    Join Date
    May 2003
    Posts
    424

    Re: itoa replacement ALMOST perfect

    I'll start using limits.h. That's a good point. I am using MSVC+ 2002. I can't find 64 bit functions anywhere.

  14. #14
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: itoa replacement ALMOST perfect

    I'd use <numeric_limits> myself but then I'm a standard C++ programmer.

  15. #15
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: itoa replacement ALMOST perfect

    Quote Originally Posted by aewarnick
    I'll start using limits.h. That's a good point. I am using MSVC+ 2002. I can't find 64 bit functions anywhere.
    According to the MSDN, the following functions are available:
    _itoa, _i64toa, _ui64toa, _itow, _i64tow, _ui64tow
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

Page 1 of 4 1234 LastLast

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

Featured