You can use an utility function to convert from the familiar hexa string to a byte[]. When used to define a final static constant, the performance cost is irrelevant.

Since Java 17

There's now java.util.HexFormat which lets you do

byte[] CDRIVES = HexFormat.of().parseHex("e04fd020ea3a6910a2d808002b30309d");

This utility class lets you specify a format which is handy if you find other formats easier to read or when you're copy-pasting from a reference source:

byte[] CDRIVES = HexFormat.ofDelimiter(":")
    .parseHex("e0:4f:d0:20:ea:3a:69:10:a2:d8:08:00:2b:30:30:9d");

Before Java 17

I'd suggest you use the function defined by Dave L in Convert a string representation of a hex dump to a byte array using Java?

byte[] CDRIVES = hexStringToByteArray("e04fd020ea3a6910a2d808002b30309d");

I insert it here for maximum readability :

public static byte[] hexStringToByteArray(String s) {
    int len = s.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                             + Character.digit(s.charAt(i+1), 16));
    }
    return data;
}
Answer from Denys Séguret on Stack Overflow
🌐
Dclunie
dclunie.com › pixelmed › software › javadoc › com › pixelmed › utils › ByteArray.html
ByteArray
java.lang.Object · com.pixelmed.utils.ByteArray · public class ByteArray extends Object · Various static methods helpful for manipulating arrays of bytes and extracting values from them. clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait ·
Discussions

Java help with byte arrays
Concatenate the strings together first? More on reddit.com
🌐 r/learnprogramming
19
2
November 13, 2016
XORing two byte arrays efficiently.
Enshittification More on reddit.com
🌐 r/csharp
33
12
August 13, 2016
Exceeding a byte[] array size limit? (Java)

No, that is the maximum size of a Java array.

Why do you want to keep a multi-gig file in memory all at once?

More on reddit.com
🌐 r/learnprogramming
6
9
February 22, 2012
(Java) Questions after converting Hex String to Byte Array
What you are seeing is related to two concepts you can Google: two's complement binary representation and Java's use of strictly signed types. The highest (rightmost) bit in any primitive integer type in Java denotes negative numbers. Bytes in Java, instead of being represented as from 0 - 255 are from -128 to 127. Internally, computers and other devices don't care, binary values are what they are. Hex value 80 denotes binary 10000000, so it is the 'first' negative number. More on reddit.com
🌐 r/learnjava
1
8
November 20, 2019
🌐
Huda Tutorials
hudatutorials.com › java › basics › java-arrays › java-byte-array
Java byte Array - byte Array in Java, initialize, String
February 7, 2022 - The byte data type comes packaged in the Java programming language and there is nothing special you have to do to get it to work . A byte is 8 bits (binary data). A byte array is an array of bytes.
🌐
GitHub
github.com › patrickfav › bytes-java
GitHub - patrickfav/bytes-java: Bytes is a utility library that makes it easy to create, parse, transform, validate and convert byte arrays in Java. It supports endianness as well as immutability and mutability, so the caller may decide to favor performance. · GitHub
Bytes is a utility library that makes it easy to create, parse, transform, validate and convert byte arrays in Java. It's main class Bytes is a collections of bytes and the main API.
Starred by 172 users
Forked by 35 users
Languages   Java
🌐
Medium
medium.com › @rrlinus5 › stream-of-bytes-internal-concept-java-831c4d94ad4c
Stream of bytes internal concept — Java | by Rakesh raj | Medium
March 2, 2024 - Stream of bytes internal concept — Java What is a byte array? A byte array in Java is simply an array of bytes. It’s a contiguous block of memory allocated to store byte values. Each element in …
🌐
DataCamp
datacamp.com › doc › java › byte
byte Keyword in Java: Usage & Examples
This example demonstrates the use of a byte array. The array byteArray is initialized with five byte values.
🌐
Baeldung
baeldung.com › home › java › java string › convert string to byte array and reverse in java
Convert String to Byte Array and Reverse in Java | Baeldung
March 17, 2024 - A String is stored as an array of Unicode characters in Java. To convert it to a byte array, we translate the sequence of characters into a sequence of bytes. For this translation, we use an instance of Charset.
Find elsewhere
🌐
W3Docs
w3docs.com › java
How do I initialize a byte array in Java? | W3Docs
If you need to change the size of the array, you can create a new array with the desired size and copy the elements of the old array to the new array using the System.arraycopy() method.
🌐
GeeksforGeeks
geeksforgeeks.org › java › convert-string-to-byte-array-in-java-using-getbytesencoding-method
Convert String to Byte Array in Java Using getBytes(encoding) Method - GeeksforGeeks
July 23, 2025 - In order to convert a string literal into a byte array, we have to first convert the sequence of characters into a sequence of bytes and for this conversion, we can use an instance of Charset. It is an abstract class present in java.nio package and it is used to define a mapping between sequence of sixteen-bit UTF-16 code units i.e sequence of character and sequence of bytes.
🌐
Baeldung
baeldung.com › home › java › java array › convert byte[] to byte[] and vice versa in java
Convert byte[] to Byte[] and Vice Versa in Java | Baeldung
July 6, 2024 - Let’s convert each byte in the array into a wrapper object using autoboxing and iterating the array in a loop: @Test void givenPrimitiveIntArray_whenConvertingAutoboxing_thenGiveExpectedResult() { Byte[] newByteArray = new Byte[PRIMITIVE_BYTE_ARRAY.length]; for (int i = 0; i < PRIMITIVE_BYTE_ARRAY.length; i++) { newByteArray[i] = PRIMITIVE_BYTE_ARRAY[i]; } Assertions.assertThat(newByteArray) .containsExactly(EXPECTED_ARRAY_VALUES); }
🌐
CodeSpeedy
codespeedy.com › home › how to initialize a byte array in java?
How to Initialize a Byte Array in Java - CodeSpeedy
October 28, 2021 - That means we can store the values ... we have seen, a byte is a combination of eight zeros and ones. A byte array is a combination of bytes values....
🌐
Baeldung
baeldung.com › home › java › java array › convert an object to a byte array in java
Convert an Object to a Byte Array in Java | Baeldung
August 7, 2025 - This class has a method named serialize(), which is used to serialize an object to a byte array: ... The above methods have parameters of type Serializable. So, our User class still needs to implement the Serializable interface, just as it did in our plain Java example.
🌐
DigitalOcean
digitalocean.com › community › tutorials › string-byte-array-java
String to byte array, byte array to String in Java | DigitalOcean
August 3, 2022 - We can use String class getBytes() method to encode the string into a sequence of bytes using the platform’s default charset. This method is overloaded and we can also pass Charset as argument. Here is a simple program showing how to convert String to byte array in java.
🌐
Baeldung
baeldung.com › home › java › java array › convert a byte array to a numeric representation in java
Convert a Byte Array to a Numeric Representation in Java | Baeldung
August 7, 2025 - In this article, we illustrated various ways to convert a byte array to a numeric value using plain Java through shift operators, ByteBuffer, and BigInteger.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-guava-bytes-toarray-method-with-examples
Java Guava | Bytes.toArray() method with Examples - GeeksforGeeks
July 11, 2025 - // Java code to show implementation of // Guava's Bytes.toArray() method import com.google.common.primitives.Bytes; import java.util.Arrays; import java.util.List; class GFG { // Driver's code public static void main(String[] args) { try { // Creating a List of Bytes List<Byte> myList = Arrays.asList((byte)2, (byte)4, null); // Using Bytes.toArray() method // to convert a List or Set of Byte // to an array of Byte.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-convert-byte-array-to-string
Java Program to Convert Byte Array to String - GeeksforGeeks
A byte is 8 bits of binary data so do byte array is an array of bytes used to store the collection of binary data. There are multiple ways to change byte array to String in Java, you can either use methods from JDK, or you can use open-source ...
Published   July 23, 2025
🌐
Reddit
reddit.com › r/learnprogramming › java help with byte arrays
r/learnprogramming on Reddit: Java help with byte arrays
November 13, 2016 -

I am struggling with some homework. Basically I need to take a bunch of info for a person and store it in a Random Access File. The problem is that the return of the method is byte[] and the only way I can do anything is with a byte[][] where I use getFamilyName.getBytes() and so on until I have all of the information. How do I make it able to store in a byte[]

🌐
Daniel Lemire's blog
lemire.me › blog › 2022 › 11 › 22 › what-is-the-size-of-a-byte-array-in-java
What is the size of a byte[] array in Java? – Daniel Lemire's blog
November 22, 2022 - Java allows you to create an array just big enough to contain 4 bytes, like so: byte[] array = new byte[4]; How much memory does this array take? If you have answered "4 bytes", you are wrong.
🌐
Mkyong
mkyong.com › home › java › java – how to join and split byte arrays, byte[]
Java - How to join and split byte arrays, byte[] - Mkyong.com
May 10, 2020 - For example, this 000102030a0b0c0d1a1b1c1d2f2f is a byte array (14 bytes) in hex representation, it is a combined of cipher (8 bytes) + nonce (4 bytes) + extra (2 bytes). 000102030a0b0c0d | 1a1b1c1d | 2f2f cipher | nonce | extra ... package com.mkyong.nio; import java.nio.ByteBuffer; public class SplitByteArrayExample { public static void main(String[] args) { byte[] input = {0x00, 0x01, 0x02, 0x03, 0x0a, 0x0b, 0x0c, 0x0d, 0x1a, 0x1b, 0x1c, 0x1d, 0x2f, 0x2f}; if (input.length != 14) { throw new IllegalArgumentException("input must be 14 bytes."); } ByteBuffer bb = ByteBuffer.wrap(input); byte[