2020/11/02 公開
・2進数を整数に変換
文字列(String型)で格納された2進数の値を、int型の整数に変換する方法を説明しています。
"00010001" → 17 "11100111" → 231
ここで説明するのは、上記のようなメソッドの作り方です。
具体的には、String型をbyte配列に変換し、そのbyte配列に格納されている'0'と'1'の値から整数の値を求めるものです。
例えば、文字列が"11001"である場合、これをbyte型の配列codeに変換すると。
code[ 0 ] = 49; // '1'のASCIIコード code[ 1 ] = 49; // '1'のASCIIコード code[ 2 ] = 48; // '0'のASCIIコード code[ 3 ] = 48; // '0'のASCIIコード code[ 4 ] = 49; // '1'のASCIIコード
となります。各文字をASCIIコードに変換された値を格納します。
このbyte配列の添え字の0から4の順に以下のような計算を行います。
int result = 0; result = (int)code[ 0 ] - 48; // resultの値 : 1 result = ( result * 2 ) + (int)code[ 1 ] - 48; // resultの値 : 3 ( 1 * 2 + 1 ) result = ( result * 2 ) + (int)code[ 2 ] - 48; // resultの値 : 6 ( 3 * 2 + 0 ) result = ( result * 2 ) + (int)code[ 3 ] - 48; // resultの値 : 12 ( 6 * 2 + 0 ) result = ( result * 2 ) + (int)code[ 4 ] - 48; // resultの値 : 25 ( 12 * 2 + 1 )
配列の添え字の小さいほうから順番に、2倍しながら足していくことで2進数の桁を1つずつ上げます。
'0'のASCIIコードが48なので、配列codeの各値から48を引いて計算します。
また、以下のように48の代わりに'0'を直接ソースコードに書いて計算させることもできます。
int result = 0; result = (int)code[ 0 ] - '0'; // resultの値 : 1 result = ( result * 2 ) + (int)code[ 1 ] - '0'; // resultの値 : 3 ( 1 * 2 + 1 ) result = ( result * 2 ) + (int)code[ 2 ] - '0'; // resultの値 : 6 ( 3 * 2 + 0 ) result = ( result * 2 ) + (int)code[ 3 ] - '0'; // resultの値 : 12 ( 6 * 2 + 0 ) result = ( result * 2 ) + (int)code[ 4 ] - '0'; // resultの値 : 25 ( 12 * 2 + 1 )
static int parseInt2( String str )が作成したメソッドで、数字の'0'と'1'以外の文字がある場合にエラーとしてint型で持てる最小値-2147483648を戻すようにしています。
StringtoValue2.java ← クリックしてダウンロードページに移動
001: public class StringtoValue2 { 002: // 2進数の文字列を10進数に変換 003: // エラーの場合、int型の最小値"Integer.MIN_VALUE"を戻す 004: static int parseInt2( String str ) 005: { 006: // 文字数を取得 007: int len = str.length(); 008: 009: // 文字数が1未満の場合、intの最小値を戻す 010: if ( 1 > len ) 011: return Integer.MIN_VALUE; 012: 013: // 文字列をbyte配列に変換 014: byte[] code = str.getBytes(); 015: 016: // 結果格納用の変数 017: int result = 0; 018: 019: // 変換するループ 020: for ( int i = 0; i < len; i++ ) { 021: switch ( code[ i ] ) { 022: case '0': 023: case '1': 024: // 数字の処理の処理 025: // 桁を1桁増やす 026: result *= 2; 027: 028: // code[i]の値を整数にキャストして足す 029: result += (int)( (byte)code[ i ] - '0' ); 030: 031: break; 032: 033: default: 034: // '0'と'1'以外の文字の場合 035: return Integer.MIN_VALUE; 036: } 037: } 038: 039: // 最後に結果を戻す 040: return result; 041: } 042: 043: 044: // メイン 045: public static void main( String[] args ) { 046: // 結果を格納する変数 047: int result; 048: 049: // 変換結果出力 050: result = parseInt2( "000" ); 051: System.out.println( result ); 052: 053: result = parseInt2( "001" ); 054: System.out.println( result ); 055: 056: result = parseInt2( "010" ); 057: System.out.println( result ); 058: 059: result = parseInt2( "1110" ); 060: System.out.println( result ); 061: 062: result = parseInt2( "10000101" ); 063: System.out.println( result ); 064: } 065: }
StringtoValue2.Javaの出力結果
0 1 2 14 133
以上です。
■関連コンテンツ
2進数 | 2で繰り上がる数値表現 |
文字列→byte配列 | Stringからbyte[] |
文字列 | 文字列について |
変数の最小値・最大値 | 変数の最小値と最大値を取得する方法を解説 |
文字数字を数値に変換 | "12345"→12345 |
■新着情報
2022.07.07 | 外部プログラムの実行 | exeファイル実行 |
2022.07.06 | 完全数 | 6=1+2+3 |