CString to int
-
Hallo
Mal ne Frage ??
Wie kann ich einen CString nach int konvertieren ?
Ich kenn atoi(...) nur leider gibt die Funktion wenn der String nicht konvertierbar war 0 zurück.
Also:
int x = atoi("Seppl") int x = atoi(CString("0"))
In beiden Fällen wäre x = 0
Das geht ja gar nicht
weil ich dann nicht weiß ob etwas schief gegangen ist. Wie macht man das richtig ?
Gruß
Martin
-
Probier mal :
int x; CString tmp; tmp.Format(_T("123")); x = _wtoi(tmp);
-
Hi
Nö
http://msdn.microsoft.com/de-de/library/yd5xkb5c(VS.80).aspx
Return Value:
Each function returns the int value produced by interpreting the input characters as a number. The return value is 0 for atoi and _wtoi, if the input cannot be converted to a value of that type.Hier gibts genau das selbe Problem. Ist blos die Unicode Version von atoi ...
Geht das wirklich nicht ? Gibts vielleicht eine Funktion die Überprüft ob der String eine Zahl ist ?
Gruß
Martin
-
Du kannst mit CString::SpanIncluding und SpanExcluding überprüfen, ob in deinem String 0-9 und . bzw. , vorhanden sind und sonst nichts.
-
EDIT: War Quatsch...
-
http://msdn.microsoft.com/de-de/library/w4z2wdyc(VS.80).aspx
The strtol function converts nptr to a long. strtol stops reading the string nptr at the first character it cannot recognize as part of a number. This may be the terminating null character, or it may be the first numeric character greater than or equal to base.