FYI here’s the javadoc for a draft of the Hex API that Alan mentioned below: http://cr.openjdk.java.net/~vinnie/8170769/javadoc.05/api/java.base/java/util/Hex.html
Thanks. > On 2 May 2018, at 10:55, Jonas Konrad <m...@yawk.at> wrote: > > I did not know about the old HexDumpEncoder. It extends an internal class > `CharacterEncoder` which seems to be pretty similar purpose-wise to what I am > suggesting with CharsetEncoder. There is also the good old > `DatatypeConverter.printHexBinary`, though it can't stream. > > But this is not really what I mean. The interesting part about doing this as > a charset is the unified API aspect of it, the fact that you can reuse > existing code utilizing Charset for hex operations. > > - Jonas > > On 05/02/2018 11:44 AM, Alan Bateman wrote: >> 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