unsigned long long to string

This is a discussion on unsigned long long to string within the C++ Programming forums, part of the General Programming Boards category; Hi, i was looking for a function to convert a unsigned long long (64 bits) to a char string. i ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    30

    unsigned long long to string

    Hi, i was looking for a function to convert a unsigned long long (64 bits) to a char string. i rather not use stringstreams for the speed and i was hoping there would be a already defined function in the CRT. ofcourse i can create a function myself, but this would be a waste of time and its always nicer if you dont have to reinvent the weel. besides, i've got to use this function many times so if its already in the CRT, why not use it?

    can anyone help?

    thanks already!

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> its always nicer if you dont have to reinvent the weel

    Erm, so why not stringstreams then? You can use this function - opposite to atoi. (I seem to link to this a lot)
    Last edited by twomers; 09-26-2006 at 12:21 PM.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Code:
    sprintf(buffer, "%lld", my_long_long_number);
    If you understand what you're doing, you're not learning anything.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    I thought long longs only existed in C99, not C++.

    BTW, itsme86, it would be %llu for an unsigned long long.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Quote Originally Posted by dwks
    BTW, itsme86, it would be %llu for an unsigned long long.
    Oops. Thanks for catching that.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    I'm almost certain that long longs only exist in C99.

    BTW, twomers:
    Quote Originally Posted by twomers
    Erm, so why not stringstreams then?
    The OP said:
    i rather not use stringstreams for the speed
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    30
    thanks for the links, i couldnt find any recourses about llu. and i knew about the stringstream. the problem is that my function is accesed by 40 threads at the same time all in a loop and they just need the unsinged int64 for a brief moment. it would be a waste of speed to use stringstreams there. everything in the app is based on speed so

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    I take back what I said about long longs only being in C99. Apparently they're planned for the next version of C++ too.
    thanks for the links, i couldnt find any recourses about llu.
    Good luck; I'm under the impression that Microsoft's implementation of *printf() doesn't support long longs, or at least it didn't for version 6.0. You could look into strtoll(). [edit] Which converts a string to a long long and not the other way around. Whoops. Maybe not. You could read the FAQ, though; there's a section on converting numbers to strings. [/edit]
    Last edited by dwks; 09-27-2006 at 01:43 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I'm not even sure stringstreams will work on your platform even if you have long long available, since that would just be a non-standard extension.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >i couldnt find any recourses about llu.
    If %lld and %llu don't work, you can try %I64d and %I64u.
    Code:
    sprintf(buffer, "%I64d", my_long_long_number);

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    30
    thanks, but llu just works fine

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Need Help Please
    By YouShallNotPass in forum C++ Programming
    Replies: 3
    Last Post: 08-22-2006, 11:22 AM
  4. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 05:20 PM
  5. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM

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