Converting 64-bit number into string

Hi everybody,

I need to convert an unsigned long long number (64 bit) into string with C++ 2005.

The number for example is: 13057201162865595358.

I want to write this value in a text file, but when i try to use ultoa to convert the number the result is 292279262.

Here is the code:

unsigned long long mt;

char buffer[64];

mt=13057201162865595358;

ultoa(mt,buffer,10);

The result in "buffer" is the number mentioned above.

Anyone can help me

Thank's in advance

Andrea



Answer this question

Converting 64-bit number into string

  • David Krmpotic

    Your input number 13057201162865595358 is more than 8 bytes. ultoa can only convert 4 byte numbers. You can convert 64 bit numbers using _i64toa.

    using namespace std;

    int _tmain(int argc, _TCHAR* argv[]) {
    long long mt;
    char buff[20];
    mt = 9223372036854775807;
    _i64toa(mt , buff, 10);
    cout<<buff<<endl;
    return 0;
    }


  • manick312938

    Thank's for your help,

    I made a little change, with your example and the number 13057201162865595358 the result is a negative number, but if I use "_ui64toa" the result in "buff" is exactly the string "13057201162865595358".

    Bye...

    Andrea


  • Troy Lundin

    Have you tried

    sspintf(buffer,"%I64d",mt)


  • jdrawmer

    A small correction. _i64toa is deprecated, _i64toa_s is a more secure version.

    Please see http://msdn2.microsoft.com/en-us/library/0we9x30h(VS.80).aspx

    Regards

    Sahir Shah


  • Converting 64-bit number into string