-
April 11th, 2006, 10:37 PM
#1
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.
-
April 11th, 2006, 10:58 PM
#2
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.
-
April 12th, 2006, 03:44 AM
#3
Re: How to convert string to unsigned long long ?
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
April 12th, 2006, 04:43 AM
#4
Re: How to convert string to unsigned long long ?
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()!
-
June 8th, 2010, 09:32 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|