Answers for "java int to binary string"

5

java integer to binary string

int n = 1000;
String s = Integer.toBinaryString(n);
Posted by: Guest on June-01-2020
3

int to binary java

String binary = Integer.toBinaryString(num);
Posted by: Guest on May-09-2020
3

java int to binary string

public static String makeBinaryString(int n) {
	StringBuilder sb = new StringBuilder();
	while (n > 0) {
    sb.append(n % 2);
		n /= 2;
	}
  	sb.reverse();  	
  	return sb.toString();
}
Posted by: Guest on May-27-2020
1

integer to binary java

Integer.toString(100,8) // prints 144 --octal representation

Integer.toString(100,2) // prints 1100100 --binary representation

Integer.toString(100,16) //prints 64 --Hex representation
Posted by: Guest on December-14-2020
0

how to get binary value in java

import java.util.*;

class Bin {

    //fields
    private Double number;

    //constructors
    public Bin(){
        number = 0.0;
    }

    public Bin(Double num){
        number = num;
    }

    //methods
    public ArrayList<String> reverseArrayList(ArrayList<String> alist) {
        
        ArrayList<String> array = new ArrayList<String>();

        int x = 0;
        int i = alist.size() - 1;

        while(x < alist.size()){
            array.add(alist.get(i));
            i -= 1;
            x += 1;
        }

        return array;
    }

	//getter methods
    public ArrayList<String> getBinVal(){
        ArrayList<String> binaryVal = new ArrayList<String>();
        ArrayList<String> array = new ArrayList<String>();
        Double num = number;
        Double sol1, check; 

        while( num > 0 ) {

            sol1 = num/2;
            check = sol1 - Math.floor(sol1);

            if(check > 0) {
                binaryVal.add("1");
                num = sol1 - 0.5;
            }

            else {
                binaryVal.add("0");
                num = sol1;
            }
        }

        while(binaryVal.size() < 4) {
            binaryVal.add("0");
        }

        array = reverseArrayList(binaryVal);
        return array;
    }

}

class MainClass {
    
    public static void main(String[] args) {
		
      	//add the number in as a command line argument
        Double num = Double.valueOf(args[0]);
		
      	//create an instance of Bin
        Bin value = new Bin(num);
		
      	//print the binary value
        System.out.println(value.getBinVal());
    }
}
Posted by: Guest on April-11-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language