How to do base conversion in Java

This is a method of performing base conversion in Java. Since it uses only the standard library, you can copy and paste it immediately.

Convert from binary

Binary-> 8

Java


final String bin = "11111110";
final int dec = Integer.parseInt(bin, 2);
final String oct = Integer.toOctalString(dec);
System.out.println(oct);

output


376

Binary-> Decimal

Java


final String bin = "11111110";
final int dec = Integer.parseInt(bin, 2);
System.out.println(dec);

output


254

Binary-> Hexagon

Java


final String bin = "11111110";
final int dec = Integer.parseInt(bin, 2);
final String hex = Integer.toHexString(dec);
System.out.println(hex);

output


fe

Binary-> 32-base

Java


final String bin = "11111110";
final int dec = Integer.parseInt(bin, 2);
final String base32 = Integer.toString(dec, 32);
System.out.println(base32);

output


7u

Binary-> n-ary

Java


final String bin = "11111110";
final int dec = Integer.parseInt(bin, 2);
final int n = 36;
final String base36 = Integer.toString(dec, n);
System.out.println(base36);

output


72

Convert from octal number

Eight-> binary

Java


final String oct = "376";
final int dec = Integer.parseInt(oct, 8);
final String bin = Integer.toBinaryString(dec);
System.out.println(bin);

output


11111110

8-> Decimal

Java


final String oct = "376";
final int dec = Integer.parseInt(oct, 8);
System.out.println(dec);

output


254

8-base-> hexadecimal

Java


final String oct = "376";
final int dec = Integer.parseInt(oct, 8);
final String hex = Integer.toHexString(dec);
System.out.println(hex);

output


fe

8-base-> 32-base

Java


final String oct = "376";
final int dec = Integer.parseInt(oct, 8);
final String base32 = Integer.toString(dec, 32);
System.out.println(base32);

output


7u

8-base-> n-ary

Java


final String oct = "376";
final int dec = Integer.parseInt(oct, 8);
final int n = 36;
final String base36 = Integer.toString(dec, n);
System.out.println(base36);

output


72

Convert from decimal

Decimal-> Binary

Java


final int dec = 254;
final String bin = Integer.toBinaryString(dec);
System.out.println(bin);

output


11111110

Decimal-> Decimal

Java


final int dec = 254;
final String oct = Integer.toOctalString(dec);
System.out.println(oct);

output


376

Decimal-> Decimal

Java


final int dec = 254;
final String hex = Integer.toHexString(dec);
System.out.println(hex);

output


fe

Decimal-> 32

Java


final int dec = 254;
final String base32 = Integer.toString(dec, 32);
System.out.println(base32);

output


7u

Decimal-> n-decimal

Java


final int dec = 254;
final int n = 36;
final String base36 = Integer.toString(dec, n);
System.out.println(base36);

output


72

Convert from hexadecimal

Hexagon-> Binary

Java


final String hex = "fe";
final int dec = Integer.parseInt(hex, 16);
final String bin = Integer.toBinaryString(dec);
System.out.println(bin);

output


11111110

Hexa-> octal

Java


final String hex = "fe";
final int dec = Integer.parseInt(hex, 16);
final String oct = Integer.toOctalString(dec);
System.out.println(oct);

output


376

Hexa-> Decimal

Java


final String hex = "fe";
final int dec = Integer.parseInt(hex, 16);
System.out.println(dec);

output


254

Hexagon-> 32 base

Java


final String hex = "fe";
final int dec = Integer.parseInt(hex, 16);
final String base32 = Integer.toString(dec, 32);
System.out.println(base32);

output


7u

Decimal-> n-decimal

Java


final String hex = "fe";
final int dec = Integer.parseInt(hex, 16);
final int n = 36;
final String base36 = Integer.toString(dec, n);
System.out.println(base36);

output


72

Convert from 32 base

32-base-> binary

Java


final String base32 = "7u";
final int dec = Integer.parseInt(base32, 32);
final String bin = Integer.toBinaryString(dec);
System.out.println(bin);

output


11111110

32-base-> octal number

Java


final String base32 = "7u";
final int dec = Integer.parseInt(base32, 32);
final String oct = Integer.toOctalString(dec);
System.out.println(oct);

output


376

32-> Decimal

Java


final String base32 = "7u";
final int dec = Integer.parseInt(base32, 32);
System.out.println(dec);

output


254

32-base-> hexadecimal

Java


final String base32 = "7u";
final int dec = Integer.parseInt(base32, 32);
final String hex = Integer.toHexString(dec);
System.out.println(hex);

output


fe

32-base-> n-ary

Java


final String base32 = "7u";
final int dec = Integer.parseInt(base32, 32);
final int n = 36;
final String base36 = Integer.toString(dec, n);
System.out.println(base36);

output


72

Convert from n-ary

n-ary-> binary

Java


final String base36 = "72";
final int n = 36;
final int dec = Integer.parseInt(base36, n);
final String bin = Integer.toBinaryString(dec);
System.out.println(bin);

output


11111110

n-ary-> octal

Java


final String base36 = "72";
final int n = 36;
final int dec = Integer.parseInt(base36, n);
final String oct = Integer.toOctalString(dec);
System.out.println(oct);

output


376

n-decimal number-> decimal number

Java


final String base36 = "72";
final int n = 36;
final int dec = Integer.parseInt(base36, n);
System.out.println(dec);

output


254

n-ary-> hexadecimal

Java


final String base36 = "72";
final int n = 36;
final int dec = Integer.parseInt(base36, n);
final String hex = Integer.toHexString(dec);
System.out.println(hex);

output


fe

n-ary-> 32-base

Java


final String base36 = "72";
final int n = 36;
final int dec = Integer.parseInt(base36, n);
final String base32 = Integer.toString(dec, 32);
System.out.println(base32);

output


7u

n-ary-> m-ary

Java


final String base36 = "72";
final int n = 36;
final int dec = Integer.parseInt(base36, n);
final int m = 3;
final String base3 = Integer.toString(dec, m);
System.out.println(base3);

output


100102

Recommended Posts

How to do base conversion in Java
How to create a data URI (base64) in Java
How to learn JAVA in 7 days
How to use classes in Java?
How to name variables in Java
How to concatenate strings in java
How to implement Kalman filter in Java
How to implement coding conventions in Java
How to embed Janus Graph in Java
How to get the date in java
How to get Class from Element in Java
How to hide null fields in response in Java
[Java] How to substitute Model Mapper in Jackson
How to solve an Expression Problem in Java
How to write Java String # getBytes in Kotlin?
[Java] How to use Map
How to create a Java environment in just 3 seconds
[Java] How to omit the private constructor in Lombok
How to lower java version
[Java] How to use Map
I want to do something like "cls" in Java
How to uninstall Java 8 (Mac)
Java --How to make JTable
How to input / output IBM mainframe files in Java?
How to use java Optional
How to minimize Java images
How to write java comments
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to display Wingdings
[Java] How to use string.format
How to use Java Map
How to set Java constants
How to generate / verify ID token in Java Memo
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
How to Git manage Java EE projects in Eclipse
How to use Java variables
Summary of how to implement default arguments in Java
How to convert Java radix
[Java] How to implement multithreading
[Java] How to use Optional ①
How to put old Java (8 series) in macOS 10.15 Catalina
How to initialize Java array
Notes on how to use regular expressions in Java
How to read your own YAML file (*****. Yml) in Java
How to use JSON data in WebSocket communication (Java, JavaScript)
Memo: [Java] How to check groupId etc. described in pom.xml
How to store a string from ArrayList to String in Java (Personal)
[Processing × Java] How to use loop 2 --- Nested structure, coordinate conversion
What happened in "Java 8 to Java 11" and how to build an environment
How to call and use API in Java (Spring Boot)
How to use Java enums (Enum) in MyBatis Mapper XML
How to dynamically switch JDK when building Java in Gradle
How to develop and register a Sota app in Java
How to simulate uploading a post-object form to OSS in Java
How to deploy Java application to Alibaba Cloud EDAS in Eclipse
How to derive the last day of the month in Java
Differences in how to handle strings between Java and Perl
How to switch Java in the OpenJDK era on Mac