java int to binary
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
- java obtain list string from list object
- Category
- Java
- Title
- treemap get order java
- Category
- Java
- Title
- query java persistence
- Category
- Java
- Title
- read a mail and its content in java mail api
- Category
- Java
- Title
- java method to capitalize first letter
- Category
- Java
- Title
- ceil function in java
- Category
- Java
- Title
- java method overriding
- Category
- Java
- Title
- int to long java
- Category
- Java
- Title
- java queue
- Category
- Java
- Title
- Java create array of array
- Category
- Java
- Title
- how to send http post create request using curl command
- Category
- Java
- Title
- java replaceall single character
- Category
- Java
- Title
- replace all punctuation in string java
- Category
- Java
- Title
- java resource file
- Category
- Java
- Title
- do statement java
- Category
- Java
- Title
- java how to make a string lowercase
- Category
- Java
- Title
- fastapi list query parameter
- Category
- Java
- Title
- NumSelfDivisors java
- Category
- Java
- Title
- random boolean java
- Category
- Java
- Title
- java map geeksforgeeks
- Category
- Java
- Title
- java boolean zen
- Category
- Java
- Title
- how to append to a string in java
- Category
- Java
- Title
- how to add an item to a list in python
- Category
- Java
- Title
- find the triplet sum in java linked list
- Category
- Java
- Title
- compile java to jar in mac
- Category
- Java
- Title
- declare bufferedreader java
- Category
- Java
- Title
- prime number program in java
- Category
- Java
- Title
- java literals w3schools
- Category
- Java
- Title
- check if a string is a substring of another string java
- Category
- Java
- Title
- java get folder content
- Category
- Java
- Title
- org.springframework.orm.jpa.EntityManagerHolder cannot be cast to org.springframework.orm.hibernate5.SessionHolder
- Category
- Java
- Title
- dates in java 8
- Category
- Java
- Title
- multiplication program java
- Category
- Java
- Title
- hashmaps java
- Category
- Java
- Title
- how to import a scanner in java
- Category
- Java
- Title
- remove part of string java
- Category
- Java
- Title
- array methods in java
- Category
- Java
- Title
- How to print in console java
- Category
- Java
- Title
- double to string java
- Category
- Java
- Title
- java put a char array into a string
- Category
- Java
- Title
- java cast duration to long
- Category
- Java
- Title
- java how to print a newline
- Category
- Java
- Title
- iterate trough linked list java
- Category
- Java
- Title
- count number of matches in two strings java
- Category
- Java
- Title
- countdown timer with seekbar
- Category
- Java
- Title
- android dynamically create layer-list with item and shape site:stackoverflow.com
- Category
- Java
- Title
- how to change the icon of a jframe
- Category
- Java
- Title
- gson parse json
- Category
- Java
- Title
- how to change tablayout current view position in android
- Category
- Java
- Title
- java map string to list
- Category
- Java
- Title
- java remove double spaces
- Category
- Java
- Title
- Java how to copy file
- Category
- Java
- Title
- convert array to list java
- Category
- Java
- Title
- numberformatexception
- Category
- Java
- Title
- how to read to into a file in java
- Category
- Java
- Title
- print map java
- Category
- Java
- Title
- how to make a text field required in android studio
- Category
- Java
- Title
- que es un length en java
- Category
- Java
- Title
- java 8 retrieve all list from object into single list and ignore duplicates
- Category
- Java
- Title
- how to make a fixed size array in java
- Category
- Java
- Title
- java length of matrix
- Category
- Java
- Title
- java localdate subtract two dates
- Category
- Java
- Title
- online money transfer andhra bank
- Category
- Java
- Title
- generate objects with for loop java
- Category
- Java
- Title
- java startActivity crash
- Category
- Java
- Title
- remove item from arraylist in java
- Category
- Java
- Title
- java array copy
- Category
- Java
- Title
- java random number
- Category
- Java
- Title
- getindexrequest elasticsearch
- Category
- Java
- Title
- python vs java
- Category
- Java
- Title
- how to remove all items from alist in java
- Category
- Java
- Title
- enum in java
- Category
- Java
- Title
- pass list to intent in android java
- Category
- Java
- Title
- install java debian 8
- Category
- Java
- Title
- java best way to concatenate strings
- Category
- Java
- Title
- java code to get all leaf nodes of a xml file
- Category
- Java
- Title
- sort elements with sortedset
- Category
- Java
- Title
- queue implementation in java using arraylist
- Category
- Java
- Title
- how to get elements of a list in java
- Category
- Java
- Title
- bigdecimal divide java
- Category
- Java
- Title
- java create a set with values
- Category
- Java
- Title
- java public static void main
- Category
- Java
- Title
- convert string to array java
- Category
- Java
- Title
- parsedouble java
- Category
- Java
- Title
- how to create a Rectangle in java
- Category
- Java
- Title
- processing library cassette
- Category
- Java
- Title
- how to delete character in string java
- Category
- Java
- Title
- adb: command not found
- Category
- Java
- Title
- set password visible in android
- Category
- Java
- Title
- how to create a circle in java
- Category
- Java
- Title
- java print syntax
- Category
- Java
- Title
- how to get length of integer in java
- Category
- Java
- Title
- if en une seul ligne java
- Category
- Java
- Title
- connect 2 package in android
- Category
- Java
- Title
- generate a qr code in java
- Category
- Java
- Title
- date to string java
- Category
- Java
- Title
- how do you concatenate an int with a string in java
- Category
- Java
- Title
- java vector push_back
- Category
- Java
- Title
- java android development get element by id
- Category
- Java
- Title
- enhanced for loop java
- Category
- Java