Hi,

The j.u.HexFormat class has methods that append hex formatted byte arrays to an Appendable such as StringBuilder.

The `toHexDigits` methods return strings with the hex values of byte, short, char, int, and long.

Similar `formatHex` methods could be added with an Appendable argument would keep the focus in HexFormat for the formatting but allows the result to be placed directly in a StringBuilder.

For example in this draft PR. https://github.com/openjdk/jdk/pull/16105

Regards, Roger


On 10/2/23 10:01 AM, 温绍锦(高铁) wrote:
StringTemplate is great and I wish I can submit PR to improve it. But I still think StringBuilder.appendHex is a basic API, not a formatted API. Just like Integer.toHexString and Long.toHexString, they are very commonly used and should be built-in.

    ------------------------------------------------------------------
    发件人:温绍锦(高铁) <shaojin.we...@alibaba-inc.com>
    发送时间:2023年10月2日(星期一) 20:46
    收件人:Mark Reinhold <mark.reinh...@oracle.com>; Claes Redestad
    <claes.redes...@oracle.com>
    抄 送:core-libs-dev@openjdk.org <core-libs-dev@openjdk.org>
    主 题:回复:Adding appendHex method to StringBuilder

    Using String Template FMT has better performance and better code
    readability. But String Template does not yet support
    for/while/if/switch. Is there any plan to enhance String Template
    to support for/while/if/switch? The following scenarios cannot be
    implemented using String.format or String Template. I think
    whether to build in support for foo(bar(x)) depends on whether it
    is a common enough method.

    ```java
    package java.net;
    class Inet6Address extends InetAddress {
        static String numericToTextFormat(byte[] src) {
            StringBuilder sb = new StringBuilder(39);
            for (int i = 0; i < (INADDRSZ / INT16SZ); i++) {
    sb.append(Integer.toHexString(((src[i<<1]<<8) & 0xff00)
              | (src[(i<<1)+1] & 0xff)));
                if (i < (INADDRSZ / INT16SZ) -1 ) {
                  sb.append(":");
                }
            }
            return sb.toString();
        }
    }
    class SocketPermission {
      private boolean authorizedIPv6(String cname, byte[] addr) {
            String authHost = "";
            InetAddress auth;
            try {
                StringBuilder sb = new StringBuilder(39);
                for (int i = 15; i >= 0; i--) {
    sb.append(Integer.toHexString(((addr[i]) & 0x0f)));
                    sb.append('.');
    sb.append(Integer.toHexString(((addr[i] >> 4) & 0x0f)));
                    sb.append('.');
                }
            ...
      }
    }
    ```
    ```java
    package sun.security.krb5.internal.crypto.dk;
    class DkCrypto {
        static String bytesToString(byte[] digest) {
            // Get character representation of digest
            StringBuilder digestString = new StringBuilder();
            for (int i = 0; i < digest.length; i++) {
                if ((digest[i] & 0x000000ff) < 0x10) {
    digestString.append('0').append(Integer.toHexString(digest[i] &
    0x000000ff));
                } else {
    digestString.append(
    Integer.toHexString(digest[i] & 0x000000ff));
                }
            }
            return digestString.toString();
        }
    }
    ```

    - wenshao

    ------------------------------------------------------------------
    发件人:Mark Reinhold <mark.reinh...@oracle.com>
    发送时间:2023年10月2日(星期一) 20:19
    收件人:Claes Redestad <claes.redes...@oracle.com>
    抄 送:温绍锦(高铁) <shaojin.we...@alibaba-inc.com>;
    core-libs-dev@openjdk.org <core-libs-dev@openjdk.org>
    主 题:Re: Adding appendHex method to StringBuilder

    2023/10/2 7:22:55 -0400, claes.redes...@oracle.com:
    > I think this goes against the grain: StringBuilder is a simple
    builder
    > for non-localized unformatted output. For formatted output there’s
    > Formatter, String.format and, with JEP 430[1], FMT."..." to help
    output
    > string data in more advanced, formatted and localized ways.

    Agreed.

    If we were to introduce a `fooBar(x)` method for every common
    `foo(bar(x))` expression in the JDK code base then our APIs
    would fast become inscrutable.

    - Mark


Reply via email to