在二进制、十进制和八进制之间转换的内置 Java 类/方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1645961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:38:12  来源:igfitidea点击:

built-in Java classes/methods to convert between binary, decimal, and octal?

java

提问by dj.

i know of several general formulas for converting between binary, decimal, and octal, but i was wondering if java has any built-in methods or classes that convert between the three. for instance, in the Integer class, there are static methods such as toBinaryString, toOctalString, etc which allow for decimal conversion, but i haven't found any to convert the other way. anyone know of anything?

我知道几个用于在二进制、十进制和八进制之间转换的通用公式,但我想知道 java 是否有任何内置方法或类可以在这三者之间进行转换。例如,在 Integer 类中,有一些静态方法,例如 toBinaryString、toOctalString 等,它们允许进行十进制转换,但我还没有找到任何可以以其他方式转换的方法。有人知道什么吗?

in particular, i am looking for methods to convert from octal and binary to decimal and between octal and binary

特别是,我正在寻找从八进制和二进制转换为十进制以及在八进制和二进制之间转换的方法

thanks! x

谢谢!X

回答by toolkit

How about: parseInt(String s, int radix)

怎么样:parseInt(String s, int radix)

parseInt("-FF", 16) returns -255
parseInt("1100110", 2) returns 102

回答by Brian Schroth

Integer.parseInt takes an optional second parameter for the base to parse it in. So to get binary,

Integer.parseInt 需要一个可选的第二个参数作为基础来解析它。所以要得到二进制,

Integer.parseInt(value, 2); 

Octal,

八进制,

Integer.parseInt(value,8);

回答by Kaleb Brasee

You're looking for Integer.parseInt(String s, int radix)and Integer.toBinaryString(int i), Integer.toOctalString(int i) and Integer.toHexString(int i). All of those are static method of the Integer class.

您正在寻找Integer.parseInt(String s, int radix)和 Integer.toBinaryString(int i)、Integer.toOctalString(int i) 和 Integer.toHexString(int i)。所有这些都是 Integer 类的静态方法。

So to turn F hex into 1111 binary:

因此,要将 F 十六进制转换为 1111 二进制:

int fifteen = Integer.parseInt("F", 16);
String fifteenInBinary = Integer.toBinaryString(fifteen);

fifteenInBinary would equal "1111";

十五InBinary 将等于“1111”;

回答by Aishwarya

Well you can use the method

那么你可以使用该方法

Integer.toBinaryString(val);

The code may look like:

代码可能如下所示:

public class NewClass4 
{
public static void main(String[] args)
{
    int value;
    String binary;
    Scanner sc=new Scanner(System.in);
    value=sc.nextInt();
    binary=Integer.toBinaryString(value);
    System.out.println(binary);
}

}

回答by Fady Nabil Yacoub

You could use the Long.toBinaryString() or Long.toOctalString() to convert a decimal to binary or octal.

您可以使用 Long.toBinaryString() 或 Long.toOctalString() 将十进制转换为二进制或八进制。

here are 2 methods that convert from binary to decimal and from octal to decimal, you could also convert from octal to decimal then to binary using these methods.

这里有两种从二进制转换为十进制和从八进制转换为十进制的方法,您也可以使用这些方法从八进制转换为十进制然后再转换为二进制。

    public long binaryToDecimal(String binary) {
    long decimal = 0;
    int power = 0;
    for (int i = binary.length() - 1; i >= 0; i--) {
        String digit = "" + binary.charAt(i);
        decimal += Integer.parseInt(digit) * Math.pow(2, power++);
    }
    return decimal;
}

public long octalToDecimal(String octal) {
    long decimal = 0;
    int power = 0;
    for (int i = octal.length() - 1; i >= 0; i--) {
        String digit = "" + octal.charAt(i);
        decimal += Integer.parseInt(digit) * Math.pow(8, power++);
    }
    return decimal;
}