<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
Hi,<br>
<br>
The j.u.HexFormat class has methods that append hex formatted byte
arrays to an Appendable such as StringBuilder.<br>
<br>
The `toHexDigits` methods return strings with the hex values of
byte, short, char, int, and long.<br>
<br>
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.<br>
<br>
For example in this draft PR.
<a class="moz-txt-link-freetext" href="https://github.com/openjdk/jdk/pull/16105">https://github.com/openjdk/jdk/pull/16105</a><br>
<br>
Regards, Roger<br>
<br>
<br>
<div class="moz-cite-prefix">On 10/2/23 10:01 AM, 温绍锦(高铁) wrote:<br>
</div>
<blockquote type="cite" cite="mid:0a97f39e-953d-4530-add3-29560d29303d.shaojin.wensj@alibaba-inc.com">
<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;">发件人:温绍锦(高铁) <a class="moz-txt-link-rfc2396E" href="mailto:shaojin.wensj@alibaba-inc.com"><shaojin.wensj@alibaba-inc.com></a></div>
<div style="clear:both;">发送时间:2023年10月2日(星期一) 20:46</div>
<div style="clear:both;">收件人:Mark Reinhold <a class="moz-txt-link-rfc2396E" href="mailto:mark.reinhold@oracle.com"><mark.reinhold@oracle.com></a>;
Claes Redestad <a class="moz-txt-link-rfc2396E" href="mailto:claes.redestad@oracle.com"><claes.redestad@oracle.com></a></div>
<div style="clear:both;">抄 送:<a class="moz-txt-link-abbreviated moz-txt-link-freetext" href="mailto:core-libs-dev@openjdk.org">core-libs-dev@openjdk.org</a>
<a class="moz-txt-link-rfc2396E" href="mailto:core-libs-dev@openjdk.org"><core-libs-dev@openjdk.org></a></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 <a class="moz-txt-link-rfc2396E" href="mailto:mark.reinhold@oracle.com"><mark.reinhold@oracle.com></a></div>
<div style="clear:both;">发送时间:2023年10月2日(星期一) 20:19</div>
<div style="clear:both;">收件人:Claes Redestad <a class="moz-txt-link-rfc2396E" href="mailto:claes.redestad@oracle.com"><claes.redestad@oracle.com></a></div>
<div style="clear:both;">抄 送:温绍锦(高铁) <a class="moz-txt-link-rfc2396E" href="mailto:shaojin.wensj@alibaba-inc.com"><shaojin.wensj@alibaba-inc.com></a>;
<a class="moz-txt-link-abbreviated
moz-txt-link-freetext" href="mailto:core-libs-dev@openjdk.org">core-libs-dev@openjdk.org</a>
<a class="moz-txt-link-rfc2396E" href="mailto:core-libs-dev@openjdk.org"><core-libs-dev@openjdk.org></a></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, <a class="moz-txt-link-abbreviated moz-txt-link-freetext" href="mailto:claes.redestad@oracle.com">claes.redestad@oracle.com</a>:<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>
</blockquote>
<br>
</body>
</html>