<div class="__aliyun_email_body_block"><div  style="line-height:1.7;font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14.0px;color:#0070c0;"><div  style="clear:both;"><span ><span >StringTemplate is great and I wish I can submit PR to improve it. But I still think StringBuilder.appendHex is a basic API</span>, not a formatted API. Just like Integer.toHexString and Long.toHexString, they are very commonly used and should be built-in.</span></div><div  style="clear:both;"><br ></div><blockquote  _quote="1" style="margin-right:.0px;margin-top:.0px;margin-bottom:.0px;font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14.0px;color:#000000;"><div  style="clear:both;">------------------------------------------------------------------</div><div  style="clear:both;">发件人:温绍锦(高铁) <shaojin.wensj@alibaba-inc.com></div><div  style="clear:both;">发送时间:2023年10月2日(星期一) 20:46</div><div  style="clear:both;">收件人:Mark Reinhold <mark.reinhold@oracle.com>; Claes Redestad <claes.redestad@oracle.com></div><div  style="clear:both;">抄 送:core-libs-dev@openjdk.org <core-libs-dev@openjdk.org></div><div  style="clear:both;">主 题:回复:Adding appendHex method to StringBuilder</div><div  style="clear:both;"><br ></div><div  class=" __aliyun_node_has_color" style="line-height:1.7;font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14.0px;color:#0070c0;"><div  style="clear:both;"><span >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? </span>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.</div><div  style="clear:both;"><span ><br ></span></div><div  style="clear:both;"><span ></span><span >```java</span><div  style="clear:both;">package java.net;</div><div  style="clear:both;">class Inet6Address extends InetAddress {</div><div  style="clear:both;">    static String numericToTextFormat(byte[] src) {</div><div  style="clear:both;">        StringBuilder sb = new StringBuilder(39);</div><div  style="clear:both;">        for (int i = 0; i < (INADDRSZ / INT16SZ); i++) {</div><div  style="clear:both;">            sb.append(Integer.toHexString(((src[i<<1]<<8) & 0xff00)</div><div  style="clear:both;">                                          | (src[(i<<1)+1] & 0xff)));</div><div  style="clear:both;">            if (i < (INADDRSZ / INT16SZ) -1 ) {</div><div  style="clear:both;">               sb.append(":");</div><div  style="clear:both;">            }</div><div  style="clear:both;">        }</div><div  style="clear:both;">        return sb.toString();</div><div  style="clear:both;">    }</div><div  style="clear:both;">}</div><div  style="clear:both;">class SocketPermission {</div><div  style="clear:both;">  private boolean authorizedIPv6(String cname, byte[] addr) {</div><div  style="clear:both;">        String authHost = "";</div><div  style="clear:both;">        InetAddress auth;</div><div  style="clear:both;">        try {</div><div  style="clear:both;">            StringBuilder sb = new StringBuilder(39);</div><div  style="clear:both;">            for (int i = 15; i >= 0; i--) {</div><div  style="clear:both;">                sb.append(Integer.toHexString(((addr[i]) & 0x0f)));</div><div  style="clear:both;">                sb.append('.');</div><div  style="clear:both;">                sb.append(Integer.toHexString(((addr[i] >> 4) & 0x0f)));</div><div  style="clear:both;">                sb.append('.');</div><div  style="clear:both;">            }</div><div  style="clear:both;">        ...</div><div  style="clear:both;">   }</div><div  style="clear:both;">}</div><div  style="clear:both;">```</div><div  style="clear:both;">```java</div><div  style="clear:both;">package sun.security.krb5.internal.crypto.dk;</div><div  style="clear:both;">class DkCrypto {</div><div  style="clear:both;">    static String bytesToString(byte[] digest) {</div><div  style="clear:both;">        // Get character representation of digest</div><div  style="clear:both;">        StringBuilder digestString = new StringBuilder();</div><div  style="clear:both;">        for (int i = 0; i < digest.length; i++) {</div><div  style="clear:both;">            if ((digest[i] & 0x000000ff) < 0x10) {</div><div  style="clear:both;">                digestString.append('0').append(Integer.toHexString(digest[i] & 0x000000ff));</div><div  style="clear:both;">            } else {</div><div  style="clear:both;">                digestString.append(</div><div  style="clear:both;">                    Integer.toHexString(digest[i] & 0x000000ff));</div><div  style="clear:both;">            }</div><div  style="clear:both;">        }</div><div  style="clear:both;">        return digestString.toString();</div><div  style="clear:both;">    }</div><div  style="clear:both;">}</div><span >```</span><span ></span></div><div  style="clear:both;"><br ></div><div  style="clear:both;">- wenshao</div><div  style="clear:both;"><br ></div><div  style="margin:14.0px .0px;"><div  style="clear:both;">------------------------------------------------------------------</div><div  style="clear:both;">发件人:Mark Reinhold <mark.reinhold@oracle.com></div><div  style="clear:both;">发送时间:2023年10月2日(星期一) 20:19</div><div  style="clear:both;">收件人:Claes Redestad <claes.redestad@oracle.com></div><div  style="clear:both;">抄 送:温绍锦(高铁) <shaojin.wensj@alibaba-inc.com>; core-libs-dev@openjdk.org <core-libs-dev@openjdk.org></div><div  style="clear:both;">主 题:Re: Adding appendHex method to StringBuilder</div><div  style="clear:both;"><br ></div>2023/10/2 7:22:55 -0400, claes.redestad@oracle.com:<br >> I think this goes against the grain: StringBuilder is a simple builder<br >> for non-localized unformatted output. For formatted output there’s<br >> Formatter, String.format and, with JEP 430[1], FMT."..." to help output<br >> string data in more advanced, formatted and localized ways.<br ><br >Agreed.<br ><br >If we were to introduce a `fooBar(x)` method for every common<br >`foo(bar(x))` expression in the JDK code base then our APIs<br >would fast become inscrutable.<br ><br >- Mark</div><div  style="line-height:20.0px;clear:both;"><br ></div></div></blockquote><div  style="line-height:20.0px;clear:both;"><br ></div></div></div>