C ++ wcstoul () - Libreria standard C ++

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

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

 Per la base 10 e la stringa larga L "29hi $" Parte numerica valida -> 29 Primo carattere dopo la parte numerica valida -> h

È definito nel file di intestazione.

prototipo di wcstoul ()

 wcstoul lungo senza segno (const wchar_t * str, wchar_t ** str_end, int base);

La funzione wcstoul () accetta una stringa ampia str, un puntatore al carattere ampio str_end e un valore intero - base come parametro.

Quindi interpreta il contenuto di una stringa ampia come un numero intero senza segno della base data e restituisce un valore int lungo senza segno.

Parametri wcstoul ()

  • str: una stringa ampia con la rappresentazione di un numero intero senza segno.
  • str_end: puntatore a un puntatore a un carattere largo. Il valore di str_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).

wcstoul () Restituisce il valore

La funzione wcstoul () restituisce:

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

Esempio 1: come funziona la funzione wcstoul ()?

 #include #include #include using namespace std; int main() ( setlocale(LC_ALL, "en_US.UTF-8"); wchar_t str1() = L"101aau16b6"; wchar_t str2() = L"59"; wchar_t *end; unsigned long value; int base = 10; value = wcstoul(str1, &end, base); wcout << L"String value = " << str1 << endl; wcout << L"Unsigned Long Int value = " << value << endl; wcout << L"End String = " << end << endl; value = wcstoul(str2, &end, base); wcout << L"String value = " << str2 << endl; wcout << L"Unsigned Long Int value = " << value << endl; wcout << L"End String = " << end << endl; return 0; )

Quando esegui il programma, l'output sarà:

 Valore stringa = 101aa ᚶ Valore Int lungo senza segno = 101 Stringa finale = aa ᚶ Valore stringa = 59 Valore Int lungo senza segno = 59 Stringa finale =

Un valore intero valido per la funzione wcstoul () è 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 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 wcstoul () con basi diverse

 #include #include #include using namespace std; int main() ( setlocale(LC_ALL, "en_US.UTF-8"); wchar_t *end; wchar_t str() = L"311bzu03feu03ff"; wcout << str << L" to Unsigned Long Int with base-5 = " << wcstoul(str, &end, 5) << endl; wcout << L"End String = " << end << endl << endl; wcout << str << L" to Unsigned Long Int with base-12 = " << wcstoul(str, &end, 12) << endl; wcout << L"End String = " << end << endl << endl; wcout << str << L" to Unsigned Long Int with base-36 = " << wcstoul(str, &end, 36) << endl; wcout << L"End String = " << end << endl << endl; return 0; )

Quando esegui il programma, l'output sarà:

 311bzϾϿ a Unsigned Long Int con base-5 = 81 End String = bzϾϿ 311bzϾϿ a Unsigned Long Int con base-12 = 5351 End String = zϾϿ 311bzϾϿ a Unsigned Long Int con base-36 = 5087231 End String = ϾϿ

La funzione wcstoul () 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 wcstoul () ha la seguente forma:

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

Quindi, a partire da questo carattere, prende più caratteri possibili che formano una rappresentazione intera valida e li converte in un valore long int senza segno.

Tutto ciò che resta della stringa dopo l'ultimo carattere valido viene ignorato e non ha alcun effetto sul risultato.

Esempio 3: funzione wcstoul () per spazi bianchi iniziali e conversione non valida

 #include #include #include using namespace std; int main() ( setlocale(LC_ALL, "en_US.UTF-8"); wchar_t *end; wcout << L" 205u03e2x to Unsigned Long Int with base-5 = " << wcstoul(L" 205u03e2x", &end, 5) << endl; wcout << L"End String = " << end << endl << endl; wcout << L"xu019cz201 to Unsigned Long Int with base-12 = " << wcstoul(L"xu019cz201", &end, 12) << endl; wcout << L"End String = " << end << endl << endl; return 0; )

Quando esegui il programma, l'output sarà:

 205Ϣx a Unsigned Long Int con base-5 = 10 End String = 5Ϣx xƜz201 a Unsigned Long Int con base-12 = 0 End String = xƜz201

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 wcstoul () con base 0

 #include #include #include using namespace std; int main() ( setlocale(LC_ALL, "en_US.UTF-8"); wchar_t *end; wcout << L"0539u1e84 to Unsigned Long Int with base-0 = " << wcstoul(L"0539u1e84", &end, 0) << endl; wcout << L"End String = " << end << endl << endl; wcout << L"0xa31u05e2 to Unsigned Long Int with base-0 = " << wcstoul(L"0xa31u05e2", &end, 0) << endl; wcout << L"End String = " << end << endl << endl; wcout << L"119xu060f to Unsigned Long Int with base-0 = " << wcstoul(L"119xu060f", &end, 0) << endl; wcout << L"End String = " << end << endl << endl; return 0; )

Quando esegui il programma, l'output sarà:

 0539Ẅ a Unsigned Long Int con base-0 = 43 End String = 9Ẅ 0xa31 ע a Unsigned Long Int con base-0 = 2609 End String = ע 119x ؏ a Unsigned Long Int con base-0 = 119 End String = x ؏

Articoli interessanti...