Java程序将二进制数转换为十进制值
java.lang.Integer类将原始类型int的值包装在对象中。类型为Integer的对象包含一个类型为int的字段。
Integer类的parseInt(Stringstr,radixr)方法将给定的字符串参数解析为第二个参数指定的基数中的有符号整数,并将其返回。
示例
import java.util.Scanner;
public class BinaryToDecimal {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a binary number ::");
String binary = sc.next();
int decimal = Integer.parseInt(binary, 2);
System.out.println("Decimal value of the given binary number is ::"+decimal);
}
}输出结果
Enter a binary number :: 101001 Decimal value of the given binary number is ::41
热门推荐
10 宝宝生病简短祝福语大全
11 盛夏的温馨祝福语简短
12 哥哥祝福语简短霸气句子
13 关于早晨的祝福语简短
14 2026猪年祝福语简短语
15 娘家除夕祝福语大全简短
16 企业励志拜年祝福语简短
17 给男生的简短祝福语
18 英文写结婚祝福语简短