atoi = string to int wie aber string to u_int?
-
Hallo, ich habe hier einen Sring, dieser enthält immer einen u_int, mit welcher Funktion kann ich diesen String in einen u_int umwandeln, bei Integer weis ich verwendet man atoi. Wer kann helfen?
-
-
Ich glaube, das geht so hier:
#include <stdio.h> #include <stdlib.h> int main() { char strX[]="123"; unsigned int iX=(unsigned int)strtoul(strX,NULL,0); printf("%u\n",iX); return 0; }
-
Ob du jetzt erstmal eine signed oder eine unsigned Variable hast, ist letztlich egal und reine Darstellungssache. Der Wert ist der gleiche. int und uint sind (unter 32-Bit-Windows) natürlich beide 4 Bytes groß.
-
Possible situation: sizeof(long) > sizeof(int) !!!
-> ULONG_MAX > UINT_MAX !!!
Look at line 27.#include <limits.h> int StrToUl( const char* str, unsigned int* num ) { char* err = NULL; unsigned long ul; char buf[128] = {0}; *num = 0; if( strlen(str) > strlen(_ultoa( UINT_MAX, buf, 10 ))) { printf("String too long !\n"); return 1; } ul = strtoul( str, &err, 10 ); if ( *err ) { printf("Not all characters could be converted. "); printf("Invalide occurance: %s\n", err ); // return 0; // return 1; user defined selection } if ( ul > UINT_MAX ) { printf("Cant convert value to unsigned int, number too big.\n"); return 1; } *num = ul; return 0; } int main() { char strX[]="123"; unsigned int iX; if( 0 == StrToUl( "222222222222222222222222222222", &iX ) ) printf("Converion successful: %u\n",iX); if( 0 == StrToUl( "123abc", &iX ) ) printf("Converion successful %u\n",iX); if( 0 == StrToUl( "456", &iX ) ) printf("Converion successful %u\n",iX); return 0; }
-
Performance last?
int StrToUI( const char* str, unsigned int* num ) { errno = 0; char* err; unsigned long ul = strtoul( str, &err, 10 ); if ( errno == ERANGE ) { // String too long return 1; } if ( *err ) { // Not all characters could be converted return 1; } #if ULONG_MAX > UINT_MAX if ( ul > UINT_MAX ) { // Cant convert value to unsigned int, number too big. return 1; } #endif *num = (unsigned int) ul; // suppress warnings return 0; }
-
Sorry, but your
if ( errno == ERANGE )
does not work on my system. Maybe my library or compiler is broken.
-
My fault !
I programmed out my brain for today I guess.
I swapped the lines to:char* err;
unsigned long ul = strtoul( str, &err, 10 );
errno = 0;So I reseted errno. *PENG!*
LordJaxom schrieb:
int StrToUI( const char* str, unsigned int* num ) { errno = 0; char* err; unsigned long ul = strtoul( str, &err, 10 ); if ( errno == ERANGE ) { // String too long return 1; } if ( *err ) { // Not all characters could be converted return 1; } #if ULONG_MAX > UINT_MAX if ( ul > UINT_MAX ) { // Cant convert value to unsigned int, number too big. return 1; } #endif *num = (unsigned int) ul; // suppress warnings return 0; }
OK well done
-
Ob ihrs glaubt oder nicht aber:
u_int = atoi(string)
klappt, habs mal testhalber ausprobiert um zu sehen was raus kommt und siehe da der komplette Zahlenbreich wird korrekt ausgegeben.
-
Das wundert mich nicht.
Ich würde aber trotzdem lieber die StrToUI Version nehmen.
Weil:MSDN:
....
Return Value
...
The return value is undefined in case of overflow.
...