Java Integer类toBinaryString()方法与示例

整数类toBinaryString()方法 (Integer class toBinaryString() method)

  • toBinaryString() method is available in java.lang package.

    toBinaryString()方法在java.lang包中可用。

  • toBinaryString() method is used to represent a binary string of the given parameter [value] of integer type as an unsigned integer in binary (base 2).

    toBinaryString()方法用于将给定参数[value]的整数类型的二进制字符串表示为二进制(基数2)中的无符号整数。

  • toBinaryString() method is a static method, it is accessible with the class name too and if we try to access the method with the class object then also we will not get an error.

    toBinaryString()方法是一个静态方法,也可以使用类名进行访问,如果我们尝试使用类对象访问该方法,那么也不会出错。

  • toBinaryString() method does not throw an exception at the time of conversion from integer to a binary string.

    从整数转换为二进制字符串时, toBinaryString()方法不会引发异常。

Syntax:

句法:

    public static String ToBinaryString(int value);

Parameter(s):

参数:

  • int value – represents the integer value to be converted.

    int value –表示要转换的整数值。

Return value:

返回值:

The return type of this method is int, it returns the binary string of the given parameter that represent the unsigned integer value.

此方法的返回类型为int ,它返回给定参数的二进制字符串,这些字符串表示无符号整数值。

Example:

例:

// Java program to demonstrate the example 
// of toBinaryString(int value) method of Integer class

public class ToBinaryStringOfIntegerClass {
    public static void main(String[] args) {
        // Variables initialization
        int i1 = 10;
        int i2 = 20;
        int i3 = 30;
        int i4 = Integer.MAX_VALUE;
        int i5 = Integer.MIN_VALUE;

        // Integer instance creation
        Integer value = new Integer(i1);

        // It represents binary string of the given
        // integer type i2 argument
        String s = value.toBinaryString(i2);

        // Display Binary String Representation
        System.out.println("value.toBinaryString(i2): " + s);

        // It represents binary string of the given
        // integer type i3 argument
        s = value.toBinaryString(i3);

        // Display Binary String Representation
        System.out.println("value.toBinaryString(i3): " + s);

        // It represents binary string of the given
        // integer type i4 argument
        s = value.toBinaryString(i4);

        // Display Binary String Representation
        System.out.println("value.toBinaryString(i4): " + s);

        // It represents binary string of the given
        // integer type i5 argument
        s = value.toBinaryString(i5);

        // Display Binary String Representation
        System.out.println("value.toBinaryString(i5): " + s);
    }
}

Output

输出量

value.toBinaryString(i2): 10100
value.toBinaryString(i3): 11110
value.toBinaryString(i4): 1111111111111111111111111111111
value.toBinaryString(i5): 10000000000000000000000000000000


翻译自: https://www.includehelp.com/java/integer-class-tobinarystring-method-with-example.aspx

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: tobinarystring方法Java中的一个方法,用于将一个整数转换为二进制字符串表示。该方法属于Integer,其语法如下: ``` public static String toBinaryString(int i) ``` 其中,i表示要转换为二进制字符串的整数。 例如,以下代码将整数10转换为二进制字符串: ``` int num = 10; String binaryString = Integer.toBinaryString(num); System.out.println(binaryString); // 输出:1010 ``` 在这个例子中,toBinaryString方法将整数10转换为二进制字符串"1010",并将其存储在变量binaryString中。最后,使用System.out.println方法将其打印出来。 需要注意的是,toBinaryString方法只能用于将整数转换为二进制字符串,如果需要将其他型的数据(如浮点数)转换为二进制字符串,需要使用其他方法或自己编写转换代码。 ### 回答2: tobinarystring方法JavaInteger的一个成员方法,它的功能是将一个整数转换成二进制字符串表示形式。 该方法接受一个整数作为参数,并返回该整数的二进制字符串表示形式。例如,如果方法的参数是10,那么它将返回字符串"1010",这个字符串表示了二进制数10的二进制形式。 使用tobinarystring方法可以方便地将一个整数以二进制的形式进行表示和处理。这在一些需要处理二进制数据的场景中非常有用。例如,在计算机网络中,IP地址通常以二进制形式进行表示,在处理这些地址时,可以使用tobinarystring方法将其转换成二进制字符串进行操作。 除了将整数转换成二进制字符串外,tobinarystring方法还可以处理负数。负数在计算机中以补码形式进行存储,tobinarystring方法可以正确地将负数转换成对应的二进制字符串表示形式。 总之,tobinarystring方法JavaInteger提供的一个实用方法,它可以将整数转换成二进制字符串形式。通过使用这个方法,我们可以方便地处理二进制数据,进行二进制数的表示和计算。 ### 回答3: tobinarystring方法是一个在Java中的内置方法,用于将一个整数转换为二进制字符串表示。这个方法接受一个整数作为参数,并返回一个对应的二进制字符串。 实现该方法的过程如下: 1. 首先,将给定的整数转换为二进制表示。在Java中,使用Integer的toBinaryString方法可以直接将整数转换为二进制字符串。 2. 确保二进制字符串的长度为32位,因为在Java中,int型的整数占32位。 3. 如果二进制字符串长度小于32位,可以使用String.format方法添加前导零,以达到32位长度。例如,可以使用String.format("%32s", binaryString).replace(' ', '0')将二进制字符串填充到32位。 下面是一个使用tobinarystring方法示例代码: ```java public class Example { public static void main(String[] args) { int number = 10; String binaryString = Integer.toBinaryString(number); String paddedBinaryString = String.format("%32s", binaryString).replace(' ', '0'); System.out.println("Binary String: " + paddedBinaryString); } } ``` 上述代码中,我们将整数10转换为二进制字符串,并在需要时填充前导零,以保证字符串长度为32位。最后,将转换后的二进制字符串打印输出。 请注意,该方法对于负数会返回32位表示的补码。为了得到原始的二进制表示,可以使用与操作符将补码转换为原码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值