Table of Contents

Java - Byte

About

Byte (Bit Octet) - Computer storage Unit (8bit) in Java

Management

Initialization

Hexa

from Number - Hexadecimal notation (0x)

0xFF = 1111 1111

byte[] bytes = new byte[] { 
                (byte) 0x01, (byte) 0xFF, (byte) 0x2E, (byte) 0x6E, (byte) 0x30
};

Decimal

A byte is also an integer between 0 (0000 0000) and 255 (1111 1111)

byte[] bytes = new byte[] { -1, -128, 1, 127 };

From File

Files.readAllBytes(path)

From String

String s = "Hello Nico";
byte[] bytes = str.getBytes(StandardCharsets.UTF_8);

Conversion

Array To String

Byte array to Java - String

byte[] bytes = { 'N', 'i', 'c', 'o' };
String s = new String(bytes);

String to Array

String s = "Hello Nico";
byte[] bytes = str.getBytes(StandardCharsets.UTF_8);

To Hexa

byte[] bytes = {'N', 'i', 'c', 'o'};
for (byte b : bytes) {
    String s = String.format("%02X", b);
    System.out.print(s);
}

or

DatatypeConverter.printHexBinary(bytes)

To int

byte b = 0x1
int i = Byte.toUnsignedInt(b)

Byte byte = new Byte(1);
int i = byte.intValue();