Charsets for hex/base64
Jonas Konrad
me at yawk.at
Wed May 2 08:35:29 UTC 2018
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.
I built such a charset a while back for fun, here is the source code:
https://gist.github.com/yawkat/6e80f21f5d418a85c3283e55b80fb607
- Jonas
More information about the core-libs-dev
mailing list