java int to binary string
Java
public static String makeBinaryString(int n) {
StringBuilder sb = new StringBuilder();
while (n > 0) {
sb.append(n % 2);
n /= 2;
}
sb.reverse();
return sb.toString();
}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());
}
}
Also in Java:
- Title
- coding with WDSL spring
- Category
- Java
- Title
- java how to serialize a file into a local server
- Category
- Java
- Title
- how to make generic arraylist in java
- Category
- Java
- Title
- java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: java.security.MessageDigest$Delegate
- Category
- Java
- Title
- spigot spawn firework
- Category
- Java
- Title
- create object of static class in java
- Category
- Java
- Title
- java string to lower case
- Category
- Java
- Title
- java get command line output
- Category
- Java
- Title
- all data types in java
- Category
- Java
- Title
- write in file java
- Category
- Java
- Title
- what is a float java
- Category
- Java
- Title
- kotlin jsonobject get nested
- Category
- Java
- Title
- not equal java
- Category
- Java
- Title
- java get current milliseconds
- Category
- Java
- Title
- array in java stack overflow
- Category
- Java
- Title
- how to create dynamic string array in java
- Category
- Java
- Title
- convert localdatetime to localdate
- Category
- Java
- Title
- how to iterate hashmap in java
- Category
- Java
- Title
- android cardview dependency
- Category
- Java
- Title
- java vector push_back
- Category
- Java
- Title
- android java how to stop users fromgoing back too much
- Category
- Java
- Title
- remove first character from string
- Category
- Java
- Title
- method overloading
- Category
- Java
- Title
- jtable get get row
- Category
- Java
- Title
- how to compare string in java
- Category
- Java
- Title
- firebase connecten
- Category
- Java
- Title
- how to write sensor data into file android studio
- Category
- Java
- Title
- find the day of the week Calendar class in java
- Category
- Java
- Title
- Java nested array hashcode
- Category
- Java
- Title
- ternary operator java
- Category
- Java
- Title
- java simple jframe example
- Category
- Java
- Title
- shorthand if java without else
- Category
- Java
- Title
- selection sort in java
- Category
- Java
- Title
- length of string java
- Category
- Java
- Title
- java 8 function supplier consumer
- Category
- Java
- Title
- jframe border
- Category
- Java
- Title
- android dismiss keyboard
- Category
- Java
- Title
- sort a map based on keys and values using java 8
- Category
- Java
- Title
- java script to detect the crome browser
- Category
- Java
- Title
- how to read to into a file in java
- Category
- Java
- Title
- java variable string
- Category
- Java
- Title
- Unrecognized option: --version Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit.
- Category
- Java
- Title
- java create clas
- Category
- Java
- Title
- how to know if String is the same java
- Category
- Java
- Title
- java append to array
- Category
- Java
- Title
- how to take space separated input in java
- Category
- Java
- Title
- java treeset
- Category
- Java
- Title
- java random number generator in range
- Category
- Java
- Title
- how to create an action listener in java
- Category
- Java
- Title
- how to print array in java using for loop
- Category
- Java
- Title
- open an existing excel file in java apache poi
- Category
- Java
- Title
- tostring() java example
- Category
- Java
- Title
- how to check wether the property exist in a object in java script
- Category
- Java
- Title
- convert int array to integer list java
- Category
- Java
- Title
- string remove last character
- Category
- Java
- Title
- how to print to console in java
- Category
- Java
- Title
- get today date in java 8
- Category
- Java
- Title
- print map java
- Category
- Java
- Title
- string to arraylist convert java
- Category
- Java
- Title
- getordefault java
- Category
- Java
- Title
- raspberry stackexchange how to install the java jdk
- Category
- Java
- Title
- objectoutputstream exemple
- Category
- Java
- Title
- que es un length en java
- Category
- Java
- Title
- android create notification
- Category
- Java
- Title
- java split string on two or more spaces except for words in quotes
- Category
- Java
- Title
- java convert String to int
- Category
- Java
- Title
- Java arraylist if you don't want to use add()
- Category
- Java
- Title
- java scanner next()
- Category
- Java
- Title
- how to make a variable unchangeable in java
- Category
- Java
- Title
- initiralizing an arraylist java
- Category
- Java
- Title
- eliminar el primer caracter de un string java
- Category
- Java
- Title
- java.lang.ClassCastException: cannot be cast to javax.servlet.Servlet
- Category
- Java
- Title
- entre clavier java
- Category
- Java
- Title
- sign flutter app android studio
- Category
- Java
- Title
- java if different ways
- Category
- Java
- Title
- java string indexof
- Category
- Java
- Title
- how to create a button in java
- Category
- Java
- Title
- java previous permutation
- Category
- Java
- Title
- java save string in text file
- Category
- Java
- Title
- how to create an array of arraylist in java
- Category
- Java
- Title
- java set example
- Category
- Java
- Title
- counting repeated characters in a string in java
- Category
- Java
- Title
- working with buttons in applet java
- Category
- Java
- Title
- java string contains char
- Category
- Java
- Title
- change number into array in java
- Category
- Java
- Title
- get last element of array java
- Category
- Java
- Title
- sdkmanager JAVA_HOME invalid directory
- Category
- Java
- Title
- how to create a random number in java
- Category
- Java
- Title
- how to get the max value of an array java
- Category
- Java
- Title
- what is a method example in java
- Category
- Java
- Title
- strong password regular expression java
- Category
- Java
- Title
- jtable font size
- Category
- Java
- Title
- convert integer array to string array
- Category
- Java
- Title
- esponente in java
- Category
- Java
- Title
- list java replace
- Category
- Java
- Title
- summary of operators java
- Category
- Java
- Title
- java shortest if else statement
- Category
- Java
- Title
- android how to split string
- Category
- Java
- Title
- java polymorphism
- Category
- Java
- Title
- how to iterate list in java selenium
- Category
- Java