Thursday 23 May 2019

Java: Convert integer to binary string


‘Integer.toBinaryString’ method returns a string representation of the integer argument as an unsigned integer base 2.

App.java
package com.sample.app;

public class App {

    public static void main(String args[]) {
        int i1 = 65535;
        int i2 = 65536;
        
        String str1 = Integer.toBinaryString(i1);
        String str2 = Integer.toBinaryString(i2);
        
        System.out.println("65535 in binary : " + str1);
        System.out.println("65536 in binary : " + str2);
    }
}

Output
65535 in binary : 1111111111111111
65536 in binary : 10000000000000000


You may like

No comments:

Post a Comment