C ++ wcstol () - Libreria standard C ++

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

La funzione wcstol () 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.

È definito nel file di intestazione.

 Per la base 10 e la stringa larga L "12abc" Parte numerica valida -> 12 Primo carattere dopo la parte numerica valida -> a

prototipo di wcstol ()

 long wcstol (const wchar_t * str, wchar_t ** str_end, int base);

La funzione wcstol () accetta una stringa larga, un puntatore a un carattere ampio e un valore intero - base come parametro, interpreta il contenuto di una stringa ampia come un numero intero della base data e restituisce un valore int lungo.

Parametri wcstol ()

  • str: una stringa ampia con la rappresentazione di un numero intero.
  • 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).

wcstol () Restituisce il valore

La funzione wcstol () restituisce:

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

Esempio 1: come funziona la funzione wcstol ()?

 #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; long value; int base = 10; value = wcstol(str1, &end, base); wcout << L"String value = " << str1 << endl; wcout << L"Long Int value = " << value << endl; wcout << L"End String = " << end << endl; value = wcstol(str2, &end, base); wcout << L"String value = " << str2 << endl; wcout << L"Long Int value = " << value << endl; wcout << L"End String = " << end << endl; return 0; )

Quando esegui il programma, l'output sarà:

 Valore stringa = 101aa ᚶ Valore lungo Int = 101 Fine stringa = aa ᚶ Valore stringa = 59 Valore lungo Int = 59 Fine stringa =

Un valore intero valido per la funzione wcstol () è 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).

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 wcstol () 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 Long Int with base-5 = " << wcstol(str, &end, 5) << endl; wcout << L"End String = " << end << endl << endl; wcout << str << L" to Long Int with base-5 = " << wcstol(str, &end, 12) << endl; wcout << L"End String = " << end << endl << endl; wcout << str << L" to Long Int with base-5 = " << wcstol(str, &end, 36) << endl; wcout << L"End String = " << end << endl << endl; return 0; )

Quando esegui il programma, l'output sarà:

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

La funzione wcstol () ignora tutti i caratteri di spazio vuoto iniziali finché non viene trovato il carattere principale diverso da spazi.

In generale, un argomento intero valido per la funzione wcstol () 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. Tutto ciò che resta della stringa dopo l'ultimo carattere valido viene ignorato e non ha alcun effetto sul risultato.

Esempio 3: funzione wcstol () 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 Long Int with base-5 = " << wcstol(L" 205u03e2x", &end, 5) << endl; wcout << L"End String = " << end << endl << endl; wcout << L"xu019cz201 to Long Int with base-12 = " << wcstol(L"xu019cz201", &end, 12) << endl; wcout << L"End String = " << end << endl << endl; return 0; )

Quando esegui il programma, l'output sarà:

 205Ϣx a Long Int con base-5 = 10 End String = 5Ϣx xƜz201 a 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 wcstol () 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 Long Int with base-0 = " << wcstol(L"0539u1e84", &end, 0) << endl; wcout << L"End String = " << end << endl << endl; wcout << L"0xa31u05e2 to Long Int with base-0 = " << wcstol(L"0xa31u05e2", &end, 0) << endl; wcout << L"End String = " << end << endl << endl; wcout << L"119xu060f to Long Int with base-0 = " << wcstol(L"119xu060f", &end, 0) << endl; wcout << L"End String = " << end << endl << endl; return 0; )

Quando esegui il programma, l'output sarà:

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

Articoli interessanti...