CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2006
    Posts
    10

    How to convert string to unsigned long long ?

    Can anyone tell me how do I convert a string to a unsigned long long type?
    Thanks.

  2. #2
    Join Date
    Mar 2006
    Posts
    151

    Re: How to convert string to unsigned long long ?

    What is an unsigned long long? Is it an unsigned 64 bit quantity? If so you might be able to use this.
    Code:
    typedef unsigned __int64 U64;
    
    U64 StringToU64(const char * sz)
    {
        U64 u64Result = 0;
        while (*sz != '\0')
        {
            u64Result *= 10 ;
            u64Result += *sz
                      -  '0';
            sz        ++    ;
        }
        return u64Result;
    }
    Cheers,
    GeoRanger
    Last edited by GeoRanger; April 12th, 2006 at 01:44 AM. Reason: Bugs in the code sample.

  3. #3
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

  4. #4
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: How to convert string to unsigned long long ?

    Quote Originally Posted by hh123
    Can anyone tell me how do I convert a string to a unsigned long long type?
    I guess that a compiler supporting unsigned long long, has an overload for operator>> on istreams.
    Thus, something like that should work:
    Code:
    template <class charT, class T>
    bool FromString(const std::basic_string<charT>& InputString, T& Value)
    {
    std::basic_istringstream<charT> in(InputString);
    return (in >> Value && in.eof());
    }
    For a C program, sscanf with %ull should also work.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  5. #5
    Join Date
    Jun 2010
    Posts
    1

    Re: How to convert string to unsigned long long ?

    http://msdn.microsoft.com/en-us/library/85zk715d.aspx

    Maybe a little late, but in case someone gets to this thread with the same problem.

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