[Java] Display the bit string stored in the byte type variable on the console

Bit string stored in byte

Java's byte type stores 8 bits (= 1 byte) of data. The bit string (binary number) stored in byte is expressed in hexadecimal and decimal as follows.

Bit string(Binary number) Hexadecimal Decimal number
00000000 00 0
01111111 7F 127
10000000 80 -128
11111111 FF -1

Display the bit string on the console

This section describes how to display in binary notation and how to display in hexadecimal notation.

--Binary notation

1:  byte b = 10;
2:  int i = Byte.toUnsignedInt(b); //Unsigned conversion
3:  String str = Integer.toBinaryString(i); //Get binary string
4:  str = String.format("%8s", str).replace(' ', '0'); //0 padding
5:  System.out.println(str);

--Hexary notation

1:  byte b = 10;
2:  System.out.print(String.format("%02X", b));

The output results of each are as follows.

Binary number: 00001010
Hexagon: 0A

If you compare the output methods, you can see that the binary notation requires various conversions.

What kind of conversion is being performed in binary notation?

I will explain what kind of conversion is being performed in binary notation. First from the second line.

2nd line: Convert from byte type to ʻint` type

2:  int i = Byte.toUnsignedInt(b); //Unsigned conversion

At this time, the point is that unsigned conversion is performed. The reason is that the upper 24 bits of the int type are set to 0.

byte type(integer) int type(integer) int type(integer)
00000001(1) 00000000000000000000000000000001(1) 00000000000000000000000000000001(1)
11111111(-1) 00000000000000000000000011111111(255) 11111111111111111111111111111111(-1)

Line 3: Get the binary string (bit string) of a ʻint` type variable

3:  String str = Integer.toBinaryString(i); //Get binary string

At this time, the high-order 0 is ignored. This is why we were doing unsigned conversions.

int type(integer) Binary string
00000000000000000000000000000001(1) 1
00000000000000000000000011111111(255) 11111111

4th line: 0 padding the acquired character string

4:  str = String.format("%8s", str).replace(' ', '0'); //0 padding

Specifically, after padding with whitespace using a formatted string, the whitespace is replaceed to 0.

Summary

When I was checking the operation of the program, I suddenly wanted to check the contents of byte [], and when I searched for a method, I struggled unexpectedly, so I left a memo on how to realize it. It's a small story, but I hope it helps someone.

Recommended Posts

[Java] Display the bit string stored in the byte type variable on the console
I tried to display the calendar on the Eclipse console using Java.
Regarding String type equivalence comparison in Java
Notes on operators using Java ~ String type ~
Execute Java code stored on the clipboard.
Console input in Java (understanding the mechanism)
The story of low-level string comparison in Java
The intersection type introduced in Java 10 is amazing (?)
About var used in Java (Local Variable Type)
Organized memo in the head (Java --Data type)
Display "Hello World" in the browser using Java
Display "Hello World" in the browser using Java
Display the list in setDetails on the screen with spring-security
Type determination in Java
Variable type in ruby
Use JLine when you want to handle keystrokes on the console character by character in Java
Let's make a calculator application in Java ~ Display the application window
[Ruby on Rails] Quickly display the page title in the browser
Convert a Java byte array to a string in hexadecimal notation
[Java] How to convert a character string from String type to byte type
I tried to convert a string to a LocalDate type in Java
How to switch Java in the OpenJDK era on Mac
[Rails] How to display information stored in the database in view
Reflection on Java string manipulation
Try functional type in Java! ①
Input to the Java console
I made the "Sunshine Ikezaki game" I saw on Twitter in Java.
[Android / Java] Understand the description type in listener settings (button installation)
Display user-defined characters on the I2C 1602 LCD with Raspberry Pi 3 & Java
Let's create a TODO application in Java 5 Switch the display of TODO
[Java] How to convert from String to Path type and get the path