C ++ strtoull () - Libreria standard C ++

La funzione strtoull () in C ++ interpreta il contenuto di una stringa come un numero intero della base specificata e restituisce il suo valore come un int lungo lungo senza segno.

La funzione strtoull () in C ++ interpreta il contenuto di una stringa come un numero intero della base specificata e restituisce il suo valore come un int lungo lungo senza segno.

Questa funzione imposta anche un puntatore in modo che punti al primo carattere dopo l'ultimo carattere valido della stringa, se presente, altrimenti il ​​puntatore viene impostato su null.

 Per la base 10 e la stringa "41aac" Parte numerica valida -> 42 Primo carattere dopo la parte numerica valida -> a

prototipo strtoull () (come dallo standard C ++ 11)

 unsigned long long int strtoull (const char * str, char ** end, int base);

La funzione strtoull () accetta una stringa, un puntatore a un carattere e un valore intero - base come parametro, interpreta il contenuto della stringa come un numero intero della base data e restituisce un valore int lungo lungo senza segno.

Questa funzione è definita nel file di intestazione.

strtoull () Parametri

  • str: Una stringa avente la rappresentazione di un numero intero.
  • end:Riferimento a un oggetto già allocato di tipo char *. Il valore di end è impostato dalla funzione sul carattere successivo in str dopo l'ultimo carattere valido. Questo parametro può anche essere un puntatore nullo, nel qual caso non viene utilizzato.
  • base:La base del valore integrale. Il set di valori validi per la base è (0, 2, 3,…, 35, 36).

strtoull () Restituisce il valore

La funzione strtoull () restituisce:

  • un valore int lungo lungo senza segno (che viene convertito dalla stringa).
  • 0 se non è stato possibile eseguire una conversione valida.

Esempio 1: come funziona la funzione strtoull ()?

 #include #include #include using namespace std; int main() ( int base = 10; char numberString() = "231ax12"; char *end; unsigned long long int number; number = strtoull(numberString, &end, base); cout << "String value = " << numberString << endl; cout << "Unsigned Long long int value = " << number << endl; cout << "End String = " << end << endl; strcpy(numberString, "231"); cout << "String value = " << numberString << endl; number = strtoull(numberString, &end, base); cout << "Unsigned Long long int value = " << number << endl; if (*end) ( cout << end; ) else ( cout << "Null pointer"; ) return 0; )

Quando esegui il programma, l'output sarà:

 Valore stringa = 231ax12 Unsigned Long long int value = 231 End String = ax12 String value = 231 Unsigned Long long int value = 231 Puntatore nullo

Un valore intero valido per la funzione strtoull () è costituito da:

  • Un segno + o - opzionale.
  • Un prefisso 0 per base ottale (si applica solo quando base = 8 o 0).
  • Un prefisso 0x o 0X per base esadecimale (si applica solo quando base = 16 o 0).
  • Una sequenza di cifre e / o alfabeti (se la base è maggiore di 10).

Se l'argomento contiene un segno meno (-) all'inizio, il numero negativo viene convertito implicitamente in un tipo int lungo lungo senza segno che è un numero positivo.

I valori validi per la base del parametro sono (0, 2, 3,…, 35, 36). Un insieme di cifre valide per la base 2 è (0, 1), per la base 3 è (0, 1, 2) e così via. Per le basi che vanno da 11 a 36, ​​le cifre valide includono gli alfabeti. L'insieme di cifre valide per la base 11 è (0, 1,…, 9, A, a), per la base 12 è (0, 1,…, 9, A, a, B, b) e così via.

Esempio 2: funzione strtoull () con basi diverse

 #include #include using namespace std; int main() ( char *end; cout << "148ax" << " to Unsigned Long Long Int with base-5 = " << strtoll("148ax", &end, 5) << endl; cout << "End String = " << end << endl << endl; cout << "148ax" << " to Unsigned Long Long Int with base-15 = " << strtoll("148ax", &end, 15) << endl; cout << "End String = " << end << endl << endl; cout << "148ax" << " to Unsigned Long Long Int with base-35 = " << strtoll("148ax", &end, 35) << endl; cout << "End String = " << end << endl << endl; return 0; )

Quando esegui il programma, l'output sarà:

 148ax a Unsigned Long Long Int con base-5 = 9 End String = 8ax 148ax a Unsigned Long Long Int con base-15 = 4405 End String = x 148ax a Unsigned Long Long Int con base-35 = 1682308 End String =

La funzione strtoull () ignora tutti i caratteri di spazio vuoto iniziali fino a quando non viene trovato il carattere principale diverso da spazi.

In generale, un argomento intero valido per la funzione strtoull () ha la seguente forma:

 (spazi) (- | +) (0 | 0x) (caratteri alfanumerici)

Quindi, a partire da questo carattere, prende il maggior numero di caratteri possibile che forma una rappresentazione intera valida e li converte in un valore int lungo e lungo. Tutto ciò che resta della stringa dopo l'ultimo carattere valido viene ignorato e non ha alcun effetto sul risultato.

Esempio 3: funzione strtoull () per la conversione di spazi bianchi, meno e non validi

 #include #include using namespace std; int main() ( char *end; cout << " 25axbz" << " to Unsigned Long Long Int with base-11 = " << strtoull(" 25axbz", &end, 11) << endl; cout << "End String = " << end << endl << endl; /* Negative value is converted to unsigned long long int type */ cout << " -110bcd" << " to Unsigned Long Long Int with base-2 = " << strtoull(" -110bcd", &end, 2) << endl; cout << "End String = " << end << endl << endl; cout << "ax110.97" << " to Unsigned Long Long Int with base-10 = " << strtoull("ax110.97", &end, 10) << endl; cout << "End String = " << end << endl << endl; return 0; )

Quando esegui il programma, l'output sarà:

 25axbz a Unsigned Long Long Int con base-11 = 307 End String = xbz -110bcd a Unsigned Long Long Int con base-2 = 18446744073709551610 End String = bcd ax110.97 a Unsigned Long Long Int con base-10 = 0 End String = ax110.97

Se la base è 0, la base numerica viene determinata automaticamente guardando il formato della stringa. Se il prefisso è 0, la base è ottale (8). Se il prefisso è 0x o 0X, la base è esadecimale (16), altrimenti la base è decimale (10).

Esempio 4: funzione strtoull () con base 0

 #include #include using namespace std; int main() ( char *end; /* octal base */ cout << "017x" << " to Unsigned Long Long Int with base-0 = " << strtoull("017x", &end, 0) << endl; cout << "End String = " << end << endl << endl; /* hexadecimal base */ cout << "0x1cg" << " to Unsigned Long Long Int with base-0 = " << strtoull("0x1cg", &end, 0) << endl; cout << "End String = " << end << endl << endl; /* decimal base */ cout << "70sxz" << " to Unsigned Long Long Int with base-0 = " << strtoull("70sxz", &end, 0) << endl; cout << "End String = " << end << endl << endl; return 0; )

Quando esegui il programma, l'output sarà:

 017x a Unsigned Long Long Int con base-0 = 15 End String = x 0x1cg a Unsigned Long Long Int con base-0 = 28 End String = g 70sxz a Unsigned Long Long Int con base-0 = 70 End String = sxz

Articoli interessanti...