私はJavaが初めてです。算術演算のみを使用して、基数 2、3、4、5、6、7、8、9、16 から基数 10 に変換するプログラムを作成したいと考えています。
キーボードから文字列を読み取り (数値が 16 進数の場合)、それを整数に変換した後、数値を数字に分割して反転する while ループを作成しました。
今、この数字を2の累乗0、1、2など(バイナリの場合)で乗算して、数値を基数10に変換する方法がわかりません。
たとえば、1001 (10 進数で 9) は、1x2(pow 0)+0x2(pow 1)+0x2(pow 2)+1x2(pow 3) のようになります。
私のコード:
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Introduceti din ce baza doriti sa convertiti numarul: 2, 3, 4, 5, 6, 7, 8, 9, 16 ");
int n = Integer.parseInt(br.readLine());
Scanner scanner = new Scanner(System.in);
System.out.println("Introduceti numarul care doriti sa fie convertit din baza aleasa ");
String inputString = scanner.nextLine();
if (n==2){
int conv = Integer.parseInt(inputString);
while (conv>0){
System.out.println (conv%10);
conv = conv/10;
}
}
}