0

我必须将以下.NET代码“转换”为JAVA

StringBuilder result = new StringBuilder();
foreach (byte current in hashBytes)
{
    result.Append(current.ToString("D").PadLeft(numberOfCharactersInStringRepresentationForByte, '0'));
}
return result.ToString();

我有一个 hashBytes 要转换的 byte[]

numberOfCharactersInStringRepresentationForByte = 3

我该怎么做?

谢谢。

到目前为止,我得到了:

StringBuilder result = new StringBuilder();
for (byte current : hashBytes)
{
        int currentUnsigned = (int) current & 0xFF; //Convert the signed byte to unsigned
        String currentUnsignedWithPadding = String.format("%3s", currentUnsigned).replace(' ', '0'); //Add tha "0" padding. AABBCCC will be 0AA0BBCCC
        result.append(currentUnsignedWithPadding);
}
System.out.println(result.toString());

它似乎工作。

请让我知道是否有一些更优雅/优化的方法可以做到这一点。

4

0 回答 0