Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Introduction

The issue of character encoding is of vital importance for Java programming when working with several systems and data sources.

In this tutorial, we’ll discuss how to convert UTF-8 encoded strings to Latin-1 encoding, which is commonly known as ISO-8859-1 encoding.

2. Problem Definition

Converting from a UTF-8 string to an ISO-8859-1 encoding environment can be surprisingly difficult. Not mapping each character in the same way may result in data corruption or loss.

To make this problem more understandable, imagine that we have UTF-8 encoded strings that should be converted to ISO-8859-1:

String string = "âabcd";

3. Direct Approach Using getBytes() Method

We can directly obtain the ISO-8859-1 bytes from the UTF-8 encoded string using the getBytes() method as follows:

byte[] expectedBytes = new byte[]{(byte) 0xE2, 0x61, 0x62, 0x63, 0x64};

@Test
void givenUtf8String_whenUsingGetByte_thenIsoBytesShouldBeEqual() {
    byte[] iso88591bytes = string.getBytes(StandardCharsets.ISO_8859_1);

    assertArrayEquals(expectedBytes, iso88591bytes);
}

In this approach, we have a UTF-8 encoded string named string containing âabcd, and the expected byte array expectedBytes represents the ISO-8859-1 encoding of this string.

We call the getBytes() method on the string object with the ISO-8859-1 charset, which returns the byte array iso88591bytes.

Finally, we use assertArrayEquals() to compare iso88591bytes with expectedBytes to ensure that the conversion results match our expectations.

This approach provides a straightforward way to obtain the desired byte array representation. 

4. Data Handling Approach

A controlled conversion approach becomes invaluable when dealing with large datasets or scenarios requiring chunked data processing. Utilizing ByteBuffer and CharBuffer from Java’s NIO package allows for decoding UTF-8 bytes into characters and subsequently encoding them into ISO-8859-1 bytes.

Let’s consider the following example:

@Test
void givenString_whenUsingByteBufferCharBufferConvertToIso_thenBytesShouldBeEqual() {
    ByteBuffer inputBuffer = ByteBuffer.wrap(string.getBytes(StandardCharsets.UTF_8));
    CharBuffer data = StandardCharsets.UTF_8.decode(inputBuffer);

    ByteBuffer outputBuffer = StandardCharsets.ISO_8859_1.encode(data);
    byte[] outputData = new byte[outputBuffer.remaining()];
    outputBuffer.get(outputData);

    assertArrayEquals(expectedBytes, outputData);
}

Here, we first wrap the UTF-8-encoded bytes of a string into a ByteBuffer. Then, using the decode() method, we decode these bytes into characters using the UTF-8 charset.

Next, we utilize the encode() method to encode the characters back into bytes using the ISO-8859-1 charset, storing the result in outputData.

This approach provides fine-grained control over the conversion process, which is particularly useful for scenarios requiring partial data handling or manipulation.

5. Conclusion

In conclusion, we discuss two approaches for converting UTF-8 encoded strings to ISO-8859-1. The direct byte conversion approach uses the getBytes() method, providing a more straightforward conversion mechanism.

On the other hand, the partial data handling approach utilizes ByteBuffer and CharBuffer, which offer finer control over the conversion process.

As always, the complete code samples for this article can be found over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments