- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Parse and Format a Number into Binary
To parse a number to binary, use the Integer.parseInt() method with binary as the first parameter and radix as 2 passed as a 2nd parameter.
Let’s say the following is our integer −
int val = 566;
Now, use the Integer.parseInt() method −
val = Integer.parseInt("11111111", 2);
Using the Integer.toString() method will format the above value into binary −
String str = Integer.toString(val, 2);
The following is an example −
Example
public class Demo { public static void main(String []args){ int val = 566; val = Integer.parseInt("11111111", 2); System.out.println(val); String str = Integer.toString(val, 2); System.out.println(str); } }
Output
255 11111111
- Related Articles
- Parse and format a number to octal in Java
- Parse and format a number to decimal in Java
- Java Program to parse Time using custom format
- Parse and format to hexadecimal in Java
- Format and Parse Date in Java
- Java Program to parse string date value with default format
- Java Program to format strings into table
- Haskell program to convert a decimal number into a binary number
- Parse and format to arbitrary radix
- Java Program to parse date and time
- Java Program to parse a mathematical expression and operators
- Java Program to convert an integer into binary
- Java Program to convert binary number to decimal number
- Java Program to format a string
- Java program to convert decimal number to binary value

Advertisements