char から int への変換のための C++ プログラム
ここでは、C++ プログラムを使用して char を int に変換する方法を見ていきます。 C++ で char を int に変換するには 6 つの方法があります。
- タイプキャストの使用。 static_cast を使用します。 sscanf() を使用します。 stoi() を使用します。 atoi() を使用します。 文字列ストリームを使用します。
これらの各方法について詳しく説明します。
1. タイプキャストの使用
方法 1:
- 変換する文字を宣言して初期化します。
- int を使用して文字を int に変換するには、文字を型キャストします。
- cout を使用して整数を出力します。
以下は、型キャストを使用して char を int 値に変換する C++ プログラムです。
C++
// C++ program to convert> // char to int (ASCII Value) using typecasting> #include> using> namespace> std;> // Driver code> int> main()> {> > char> ch => 'A'> ;> > cout < <> int> (ch);> > return> 0;> }> |
出力
65
時間計算量は O(1) であり、補助空間も O(1) です
数値を整数値にタイプキャストする必要がある場合は、48 または「0」を減算してから、数値を int にタイプキャストできます。
以下は、型キャストを使用して char を整数値に変換する C++ プログラムです。
C++
// C++ program to convert> // char to int (integer value) using typecasting> #include> using> namespace> std;> // Driver code> int> main()> {> > char> ch => '5'> ;> > // Subtracting 48 will produce desired results> > cout < <> int> (ch) - 48 < <> '
'> ;> > // Also subtracting '0' will result in same output> > cout < <> int> (ch -> '0'> );> > return> 0;> }> // This code is contributed by Susobhan Akhuli> |
出力
5 5
方法 2:
- 変換する文字を宣言して初期化します。
- 別の変数を int N として宣言し、文字 ch を N に割り当てます。
- cout を使用して整数を出力します。
以下は、型キャストを使用して char を int 値に変換する C++ プログラムです。
C++
// C++ program to convert> // char to int (ASCII value) using typecasting> #include> using> namespace> std;> // Driver code> int> main()> {> > char> ch => 'a'> ;> > int> N => int> (ch);> > cout < < N;> > return> 0;> }> |
出力
97
2. static_cast の使用
static_cast 関数を使用して文字を整数に変換できます。以下は、static_cast を使用して char を int 値に変換する C++ プログラムです。
C++
// C++ program to convert char> // to int (ASCII Value) using static_cast> #include> using> namespace> std;> // Driver code> int> main()> {> > char> ch => 'A'> ;> > int> N => static_cast> <> int> >(ch);>> > cout < < N;> > return> 0;> }> |
出力
65
3. sscanf の使用
s からデータを読み取り、パラメータ形式の追加引数で指定された場所にデータを格納します。以下は、sscanf() を使用して char を int に変換する C++ プログラムです。
C++
// C++ program to convert char> // to int using sscanf()> #include> using> namespace> std;> // Driver code> int> main()> {> > const> char> *s => '1234'> ;> > int> x;> > sscanf> (s,> '%d'> , &x);> > cout < <> '
The integer value of x : '> < < x;> > return> 0;> }> |
出力
The integer value of x : 1234
4. ストイの使用
C++ の stoi() 関数は、文字列を整数値に変換します。以下は、stoi() を使用して char を int に変換する C++ プログラムです。
C++
// C++ program to convert char> // to int using stoi()> #include> #include> using> namespace> std;> // Driver code> int> main()> {> > char> s1[] => '45'> ;> > int> x = stoi(s1);> > cout < <> 'The integer value of x : '> < < x;> > return> 0;> }> |
出力
The integer value of x : 45
5.atoiの使い方
実行が成功すると、atoi() メソッドは変換された整数値を返します。指定された文字列を整数に変換できない場合は、0 が返されます。 以下は、atoi() を使用して char を int に変換する C++ プログラムです。
C++
// C++ program to convert char> // to int using atoi()> #include> using> namespace> std;> // Driver code> int> main()> {> > const> char> *str => '1234'> ;> > int> y => atoi> (str);> > cout < <> '
The integer value of y :'> < < y;> > return> 0;> }> |
出力
The integer value of y :1234
6. 文字列ストリームの使用
stringstream は文字列オブジェクトをストリームに接続し、ストリーム (cin など) であるかのように読み取ることができます。 Stringstream には、sstream ヘッダー ファイルを含める必要があります。 stringstream クラスは、入力を処理するときに便利です。
以下は、文字列ストリームを使用して char を int に変換する C++ プログラムです。
C++
// C++ program to convert char> // to int using string stream> #include> #include> #include> using> namespace> std;> // Driver code> int> main()> {> > stringstream string;> > string < <> '5'> ;> > int> n;> > string>> ん;>> > cout < <> 'Integer value is: '> < < n;> > return> 0;> }> |
出力
Integer value is: 5
6. メソッド: char 値に 0 を加算して int に変換する
C++
// C++ program to convert> // char to int using typecasting by adding zero> #include> using> namespace> std;> > //Driver code> int> main()> {> > char> charvalue => 'a'> ;> > int> number = (> int> (charvalue)+0);> > cout < < number;> > return> 0;> }> > //this code is contributed by uomkar369> |
出力
97