Converting a byte array into a hex string

Converting a byte array into a hex string



Surprisingly (to me), this code does not do what I want:


fun ByteArray.toHexString() : String
return this.joinToString("") it.toString(16)



Turns out Byte is signed, so you get negative hex representations for individual bytes, which leads to a completely bogus end result.


Byte



Also, Byte.toString won't pad leading zeroes, which you'd want here.


Byte.toString



What is the simplest (no additional libraries, ideally no extensions) resp. most efficient fix?






... can you give me an input for which you get the bogus output?

– Roland
Sep 7 '18 at 15:17






@Roland 0xFF.toByte().toString(16)

– Moira
Sep 7 '18 at 15:17



0xFF.toByte().toString(16)






@Roland My hint was seeing - in SHA-256 hashes.

– Raphael
Sep 7 '18 at 15:18


-






tried all single byte strings from 0x00 upto 0xFF. All variants on this page produce the same result.. what am I missing here?

– Roland
Sep 7 '18 at 15:31



0x00


0xFF






just wondering: how do you get your ByteArray?

– Roland
Sep 7 '18 at 15:43


ByteArray




3 Answers
3



As I am on Kotlin 1.3 you may also be interested in the UByte soon (note that it's an experimental feature. See also Kotlin 1.3M1 and 1.3M2 announcement)


UByte



E.g.:


@ExperimentalUnsignedTypes // just to make it clear that the experimental unsigned types are used
fun ByteArray.toHexString() = asUByteArray().joinToString("") it.toString(16).padStart(2, '0')



The formatting option is probably the nicest other variant (but maybe not that easily readable... and I always forget how it works, so it is definitely not so easy to remember (for me :-)):


fun ByteArray.toHexString() = joinToString("") String.format("%02x", it)



printf does what we want here:


printf


fun ByteArray.toHexString() : String
return this.joinToString("")
java.lang.String.format("%02x", it)







I doubt that you need to spell out java.lang.String

– msrd0
Sep 7 '18 at 23:54


java.lang.String






@msrd0 There is no Java tag on the question. Using KotlinJS I can appreciate when I can rule out an answer like that at the first glance.

– Zackline
Nov 5 '18 at 20:34






@msrd0 I think I wanted to make sure not to pick Kotlin's String, but it does indeed seem to be unnecessary.

– Raphael
Nov 5 '18 at 22:07


String






@Zackline Interesting point. Has any convention for tagging Kotlin questions regarding JVM/JS/Android/Native emerged yet?

– Raphael
Nov 5 '18 at 22:07


fun ByteArray.toHexString() = joinToString("")
Integer.toUnsignedString(java.lang.Byte.toUnsignedInt(it), 16).padStart(2, '0')



Fortunately, Java has toUnsignedString methods on Integer and Long. UNfortunately, these methods are only on Integer and Long, so you need to convert each byte first (using Byte#toUnsignedInt).


toUnsignedString


Integer


Long


Integer


Long


Byte#toUnsignedInt



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế