Java - Byte

> Procedural Languages > Java

1 - About

Advertising

3 - Management

3.1 - Initialization

3.1.1 - Hexa

from Number - Hexadecimal notation (0x)

0xFF = 1111 1111

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

3.1.2 - Decimal

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

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

3.1.3 - From File

Files.readAllBytes(path)

3.1.4 - From String

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

3.2 - Conversion

3.2.1 - Array To String

Byte array to Java - String

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

3.2.2 - String to Array

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

3.2.3 - 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)

3.2.4 - To int

byte b = 0x1
int i = Byte.toUnsignedInt(b)
 
Byte byte = new Byte(1);
int i = byte.intValue();
lang/java/byte.txt · Last modified: 2019/07/10 09:27 by gerardnico