1

I don't know if somebody has already answered this question. If it's the case, then, please for the redondancy.

I would like to transform this function into Kotlin function. Thanks for the help.

private final static String HEX = "XXXXXXXXX";
private static void appendHex(StringBuffer sb, byte b) {
    sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
}
5
  • 1
    could it be that HEX actually rather contains something like 0123456789ABCDEF? if so... what about: String.format("%02X", b) instead? Commented Mar 21, 2019 at 9:29
  • @Roland. Yes it contains. But it is the content of the function that is very important to me! Commented Mar 21, 2019 at 10:00
  • I am not sure whether I understood you correctly... sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f)); is what you want to translate to Kotlin right? But if it is already simplifiable to sb.append(String.format("%02X", b)), that's not ok for you? Commented Mar 21, 2019 at 10:11
  • ok. i see what you claim. That's ok too. Commented Mar 21, 2019 at 11:11
  • There's a chance that you want to keep this static function in Java so that you can still keep using Byte with >>. Commented Mar 21, 2019 at 11:36

3 Answers 3

4

I suggest using an extension function for that:

private const val HEX = "XXXXXXXXX"
fun StringBuffer.appendHex(b: Byte): StringBuffer {
    val i = b.toInt()
    append(HEX[i shr 4 and 0x0f])
    append(HEX[i and 0x0f])
    return this
}

I also made the function return this again so you can even call it in a chain:

myStringBuffer
   .append(someThing)
   .appendHex(myByte)
   .append(someThingElse)
Sign up to request clarification or add additional context in comments.

Comments

2

If it's just a hex string representation you need (and that's also what I get from your comment), you may just want to use the following instead:

fun StringBuffer.appendHex(b : Byte) = append("%02X".format(b))

Usage is then the same as also Lino has shown:

StringBuffer()
    .append("some text")
    .appendHex(someByte)

or if you need to keep your current signature:

companion object {
  @JvmStatic
  fun appendHex(s : StringBuffer, b : Byte) {
    s.append("%02X".format(b))
  }
}

2 Comments

You could also use "%02X".format(b), which does the exact same as String.format()
@Lino thanks! didn't know that one ;-) but to be honest, I didn't even care so far as I used a static import for the String.format and that was already short enough ;-) but I will remember now ;-)
1
companion object {
    private const val HEX = "XXXXXXXXX"

    @JvmStatic
    private fun appendHex(sb: StringBuffer, b: Int) {
        sb.append(HEX[b shr 4 and 0x0f]).append(HEX[b and 0x0f])
    }
}

Please note that I had to change the Byte to Int, because Byte doesn't seem to have shr and and defined for some unknown reason.

No, I really don't know why bitwise operators would not be defined over byte.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.