Charsets for hex/base64
Alan Bateman
Alan.Bateman at oracle.com
Wed May 2 09:44:28 UTC 2018
On 02/05/2018 09:35, Jonas Konrad wrote:
> Hi,
>
> Conceptually, a 'charset' (in java) is a pair of transformations from
> bytes to utf-16 code units and vice versa. Could it be useful to have
> charset implementations that convert from bytes to the hex (or base64)
> representations of those? The idea is as follows:
>
> "0a0b0c".getBytes(HexCharset.getInstance()) = new byte[] { 0x0a, 0x0b,
> 0x0c }
> new String(new byte[] { 0x0a, 0x0b, 0x0c }, HexCharset.getInstance())
> = "0a0b0c"
>
> The motivation behind this idea is that there are lots of APIs that
> provide efficient transformations between chars and bytes using
> charsets, but converting to/from hex for debugging is usually more
> involved. One example of this is netty ByteBuf.toString (
> https://netty.io/4.0/api/io/netty/buffer/ByteBuf.html#toString-int-int-java.nio.charset.Charset-
> ) - it would be really convenient to be able to just plug in a hex
> charset to get nice debug output.
>
> Of course this is only one example and there are other ways to get hex
> output from ByteBuf in netty in particular, but I still think that a
> hex charset would be an interesting tool to reuse code. I understand
> this is a somewhat dubious use of a Charset, but I'd like to hear
> other thoughts on it.
>
Have you looked at the Hex API proposed in JDK-8170769? It needs to be
dusted off but the last iteration discussed here got to the point where
it provides conversion of binary data to/from hex string representation.
It also supported both bulk and stream operations.
-Alan
More information about the core-libs-dev
mailing list