0%

Convert Byte Array to String

csongyu/byte-to-string

Text Data

1
2
3
4
5
6
7
8
9
10
11
public class CharacterByteToStringServiceImpl implements ByteToStringService {
@Override
public String encode(final byte[] bytes, final Charset charset) {
return new String(bytes, charset);
}

@Override
public byte[] decode(final String str, final Charset charset) {
return str.getBytes(charset);
}
}

Binary Data

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class BinaryByteToStringServiceImpl implements ByteToStringService {
@Override
public String encode(final byte[] bytes, final Encoding encoding) {
return switch (encoding) {
case HEX -> Hex.encodeHexString(bytes);
case BASE_64 -> Base64.getEncoder().encodeToString(bytes);
case ISO_8859_1 -> new String(bytes, StandardCharsets.ISO_8859_1);
};
}

@Override
public byte[] decode(final String str, final Encoding encoding) throws DecoderException {
return switch (encoding) {
case HEX -> Hex.decodeHex(str);
case BASE_64 -> Base64.getDecoder().decode(str);
case ISO_8859_1 -> str.getBytes(StandardCharsets.ISO_8859_1);
};
}
}

错误场景

1
2
3
4
5
6
7
8
9
10
// UTF-8 编码的字节 unique byte sequence 和字符 unique character sequence 不能一一对应
// 使用 Hex、Base64、ISO-8859-1 编码处理 Binary Data 场景
@Test
public void givenBinaryData_whenUseUtf8ConvertBytesToStringThenBackToBytes_thenIncorrect() throws IOException {
final byte[] source = Files.readAllBytes(Paths.get("src/test/resources/profile.png"));
// byteToStringService = new CharacterByteToStringServiceImpl();
final String str = this.byteToStringService.encode(source, StandardCharsets.UTF_8);
final byte[] target = this.byteToStringService.decode(str, StandardCharsets.UTF_8);
assertNotEquals(Arrays.toString(source), Arrays.toString(target));
}