toHexString

Common
JVM
JS
Native
1.9
@ExperimentalStdlibApi fun ByteArray.toHexString(
    format: HexFormat = HexFormat.Default
): String

(source)
@ExperimentalStdlibApi @ExperimentalUnsignedTypes fun UByteArray.toHexString(
    format: HexFormat = HexFormat.Default
): String

(source)

Formats bytes in this array using the specified format.

Note that only the HexFormat.upperCase and HexFormat.bytes properties of the format instance affect the formatting result of this byte array.

Each byte in the array is converted into two hexadecimal digits. The first digit represents the most significant 4 bits, and the second digit represents the least significant 4 bits of the byte. The HexFormat.upperCase property determines whether upper-case (0-9, A-F) or lower-case (0-9, a-f) hexadecimal digits are used for this conversion. The HexFormat.bytes property specifies the strings that prefix and suffix each byte representation, and defines how these representations are arranged.

Refer to HexFormat.BytesHexFormat for details about the available format options and their impact on formatting.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val data = byteArrayOf(0xDE.toByte(), 0x2D, 0x02, 0xC0.toByte(), 0x5C, 0x0E)

// Using the default format
println(data.toHexString()) // de2d02c05c0e

// Using a custom format
val format = HexFormat {
    upperCase = true
    bytes {
        bytesPerLine = 4
        byteSeparator = " " // One space
        bytePrefix = "0x"
    }
}
println(data.toHexString(format)) // 0xDE 0x2D 0x02 0xC0\n0x5C 0x0E

// Formatting a segment of the byte array
println(data.toHexString(startIndex = 2, endIndex = 5, format)) // 0x02 0xC0 0x5C

// Formatting unsigned bytes with the same binary representations yields the same string
val unsignedData = data.toUByteArray()
println(unsignedData.toHexString(format)) // 0xDE 0x2D 0x02 0xC0\n0x5C 0x0E
//sampleEnd
}

Parameters

format - the HexFormat to use for formatting, HexFormat.Default by default.

Exceptions

IllegalArgumentException - if the result length exceeds the maximum capacity of String.

Return the result of formatting this array using the specified format.

Common
JVM
JS
Native
1.9
@ExperimentalStdlibApi fun ByteArray.toHexString(
    startIndex: Int = 0,
    endIndex: Int = size,
    format: HexFormat = HexFormat.Default
): String

(source)
@ExperimentalStdlibApi @ExperimentalUnsignedTypes fun UByteArray.toHexString(
    startIndex: Int = 0,
    endIndex: Int = size,
    format: HexFormat = HexFormat.Default
): String

(source)

Formats bytes in this array using the specified format.

Note that only the HexFormat.upperCase and HexFormat.bytes properties of the format instance affect the formatting result of this byte array.

Each byte in the array is converted into two hexadecimal digits. The first digit represents the most significant 4 bits, and the second digit represents the least significant 4 bits of the byte. The HexFormat.upperCase property determines whether upper-case (0-9, A-F) or lower-case (0-9, a-f) hexadecimal digits are used for this conversion. The HexFormat.bytes property specifies the strings that prefix and suffix each byte representation, and defines how these representations are arranged.

Refer to HexFormat.BytesHexFormat for details about the available format options and their impact on formatting.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val data = byteArrayOf(0xDE.toByte(), 0x2D, 0x02, 0xC0.toByte(), 0x5C, 0x0E)

// Using the default format
println(data.toHexString()) // de2d02c05c0e

// Using a custom format
val format = HexFormat {
    upperCase = true
    bytes {
        bytesPerLine = 4
        byteSeparator = " " // One space
        bytePrefix = "0x"
    }
}
println(data.toHexString(format)) // 0xDE 0x2D 0x02 0xC0\n0x5C 0x0E

// Formatting a segment of the byte array
println(data.toHexString(startIndex = 2, endIndex = 5, format)) // 0x02 0xC0 0x5C

// Formatting unsigned bytes with the same binary representations yields the same string
val unsignedData = data.toUByteArray()
println(unsignedData.toHexString(format)) // 0xDE 0x2D 0x02 0xC0\n0x5C 0x0E
//sampleEnd
}

Parameters

startIndex - the beginning (inclusive) of the subrange to format, 0 by default.

endIndex - the end (exclusive) of the subrange to format, size of this array by default.

format - the HexFormat to use for formatting, HexFormat.Default by default.

Exceptions

IndexOutOfBoundsException - when startIndex or endIndex is out of range of this array indices.

IllegalArgumentException - when startIndex > endIndex.

IllegalArgumentException - if the result length exceeds the maximum capacity of String.

Return the result of formatting this array using the specified format.

Common
JVM
JS
Native
1.9
@ExperimentalStdlibApi fun Byte.toHexString(
    format: HexFormat = HexFormat.Default
): String

(source)

Formats this Byte value using the specified format.

Note that only the HexFormat.upperCase and HexFormat.number properties of the format instance affect the formatting result of this numeric value.

This function converts the Byte value into its hexadecimal representation by mapping each four-bit chunk of its binary representation to the corresponding hexadecimal digit, starting from the most significant bits. The HexFormat.upperCase property determines whether upper-case (0-9, A-F) or lower-case (0-9, a-f) hexadecimal digits are used for this conversion. The HexFormat.number property adjusts the length of the hexadecimal representation by adding or removing leading zeros as needed, and specifies the strings that prefix and suffix the resulting representation.

Refer to HexFormat.NumberHexFormat for details about the available format options and their impact on formatting.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
// Using the default format
val value: Byte = 58
println(value.toHexString()) // 3a

// Converts the Byte to an unsigned hexadecimal representation
println((-1).toByte().toHexString()) // ff

// Using a custom format
val format = HexFormat {
    upperCase = true
    number {
        prefix = "\\u" // A Unicode escape prefix
        minLength = 4
    }
}
println(value.toHexString(format)) // \\u003A

// Formatting an unsigned value with the same binary representations yields the same string
val uValue: UByte = 58u
println(uValue.toHexString(format)) // \\u003A
//sampleEnd
}

Parameters

format - the HexFormat to use for formatting, HexFormat.Default by default.

Return the result of formatting this value using the specified format.

Common
JVM
JS
Native
1.9
@ExperimentalStdlibApi fun Short.toHexString(
    format: HexFormat = HexFormat.Default
): String

(source)

Formats this Short value using the specified format.

Note that only the HexFormat.upperCase and HexFormat.number properties of the format instance affect the formatting result of this numeric value.

This function converts the Short value into its hexadecimal representation by mapping each four-bit chunk of its binary representation to the corresponding hexadecimal digit, starting from the most significant bits. The HexFormat.upperCase property determines whether upper-case (0-9, A-F) or lower-case (0-9, a-f) hexadecimal digits are used for this conversion. The HexFormat.number property adjusts the length of the hexadecimal representation by adding or removing leading zeros as needed, and specifies the strings that prefix and suffix the resulting representation.

Refer to HexFormat.NumberHexFormat for details about the available format options and their impact on formatting.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
// Using the default format
val value: Short = 58
println(value.toHexString()) // 003a

// Converts the Short to an unsigned hexadecimal representation
println((-1).toShort().toHexString()) // ffff

// Using a custom format
val format = HexFormat {
    upperCase = true
    number {
        prefix = "0x"
        removeLeadingZeros = true
    }
}
println(value.toHexString(format)) // 0x3A

// Formatting an unsigned value with the same binary representations yields the same string
val uValue: UShort = 58u
println(uValue.toHexString(format)) // 0x3A
//sampleEnd
}

Parameters

format - the HexFormat to use for formatting, HexFormat.Default by default.

Return the result of formatting this value using the specified format.

Common
JVM
JS
Native
1.9
@ExperimentalStdlibApi fun Int.toHexString(
    format: HexFormat = HexFormat.Default
): String

(source)

Formats this Int value using the specified format.

Note that only the HexFormat.upperCase and HexFormat.number properties of the format instance affect the formatting result of this numeric value.

This function converts the Int value into its hexadecimal representation by mapping each four-bit chunk of its binary representation to the corresponding hexadecimal digit, starting from the most significant bits. The HexFormat.upperCase property determines whether upper-case (0-9, A-F) or lower-case (0-9, a-f) hexadecimal digits are used for this conversion. The HexFormat.number property adjusts the length of the hexadecimal representation by adding or removing leading zeros as needed, and specifies the strings that prefix and suffix the resulting representation.

Refer to HexFormat.NumberHexFormat for details about the available format options and their impact on formatting.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
// Using the default format
val value = 58
println(value.toHexString()) // 0000003a

// Converts the Int to an unsigned hexadecimal representation
println((-1).toHexString()) // ffffffff

// Using a custom format
val format = HexFormat {
    upperCase = true
    number {
        prefix = "#"
        removeLeadingZeros = true
        minLength = 6
    }
}
println(value.toHexString(format)) // #00003A

// Formatting an unsigned value with the same binary representations yields the same string
val uValue = 58u
println(uValue.toHexString(format)) // #00003A
//sampleEnd
}

Parameters

format - the HexFormat to use for formatting, HexFormat.Default by default.

Return the result of formatting this value using the specified format.

Common
JVM
JS
Native
1.9
@ExperimentalStdlibApi fun Long.toHexString(
    format: HexFormat = HexFormat.Default
): String

(source)

Formats this Long value using the specified format.

Note that only the HexFormat.upperCase and HexFormat.number properties of the format instance affect the formatting result of this numeric value.

This function converts the Long value into its hexadecimal representation by mapping each four-bit chunk of its binary representation to the corresponding hexadecimal digit, starting from the most significant bits. The HexFormat.upperCase property determines whether upper-case (0-9, A-F) or lower-case (0-9, a-f) hexadecimal digits are used for this conversion. The HexFormat.number property adjusts the length of the hexadecimal representation by adding or removing leading zeros as needed, and specifies the strings that prefix and suffix the resulting representation.

Refer to HexFormat.NumberHexFormat for details about the available format options and their impact on formatting.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
// Using the default format
val value = 58L
println(value.toHexString()) // 000000000000003a

// Converts the Long to an unsigned hexadecimal representation
println((-1L).toHexString()) // ffffffffffffffff

// Using a custom format
val format = HexFormat {
    upperCase = true
    number.removeLeadingZeros = true
}
println(value.toHexString(format)) // 3A

// Formatting an unsigned value with the same binary representations yields the same string
val uValue = 58uL
println(uValue.toHexString(format)) // 3A
//sampleEnd
}

Parameters

format - the HexFormat to use for formatting, HexFormat.Default by default.

Return the result of formatting this value using the specified format.

Common
JVM
JS
Native
1.9
@ExperimentalStdlibApi fun UByte.toHexString(
    format: HexFormat = HexFormat.Default
): String

(source)

Formats this UByte value using the specified format.

Note that only the HexFormat.upperCase and HexFormat.number properties of the format instance affect the formatting result of this numeric value.

This function converts the UByte value into its hexadecimal representation by mapping each four-bit chunk of its binary representation to the corresponding hexadecimal digit, starting from the most significant bits. The HexFormat.upperCase property determines whether upper-case (0-9, A-F) or lower-case (0-9, a-f) hexadecimal digits are used for this conversion. The HexFormat.number property adjusts the length of the hexadecimal representation by adding or removing leading zeros as needed, and specifies the strings that prefix and suffix the resulting representation.

Refer to HexFormat.NumberHexFormat for details about the available format options and their impact on formatting.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
// Using the default format
val value: Byte = 58
println(value.toHexString()) // 3a

// Converts the Byte to an unsigned hexadecimal representation
println((-1).toByte().toHexString()) // ff

// Using a custom format
val format = HexFormat {
    upperCase = true
    number {
        prefix = "\\u" // A Unicode escape prefix
        minLength = 4
    }
}
println(value.toHexString(format)) // \\u003A

// Formatting an unsigned value with the same binary representations yields the same string
val uValue: UByte = 58u
println(uValue.toHexString(format)) // \\u003A
//sampleEnd
}

Parameters

format - the HexFormat to use for formatting, HexFormat.Default by default.

Return the result of formatting this value using the specified format.

Common
JVM
JS
Native
1.9
@ExperimentalStdlibApi fun UShort.toHexString(
    format: HexFormat = HexFormat.Default
): String

(source)

Formats this UShort value using the specified format.

Note that only the HexFormat.upperCase and HexFormat.number properties of the format instance affect the formatting result of this numeric value.

This function converts the UShort value into its hexadecimal representation by mapping each four-bit chunk of its binary representation to the corresponding hexadecimal digit, starting from the most significant bits. The HexFormat.upperCase property determines whether upper-case (0-9, A-F) or lower-case (0-9, a-f) hexadecimal digits are used for this conversion. The HexFormat.number property adjusts the length of the hexadecimal representation by adding or removing leading zeros as needed, and specifies the strings that prefix and suffix the resulting representation.

Refer to HexFormat.NumberHexFormat for details about the available format options and their impact on formatting.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
// Using the default format
val value: Short = 58
println(value.toHexString()) // 003a

// Converts the Short to an unsigned hexadecimal representation
println((-1).toShort().toHexString()) // ffff

// Using a custom format
val format = HexFormat {
    upperCase = true
    number {
        prefix = "0x"
        removeLeadingZeros = true
    }
}
println(value.toHexString(format)) // 0x3A

// Formatting an unsigned value with the same binary representations yields the same string
val uValue: UShort = 58u
println(uValue.toHexString(format)) // 0x3A
//sampleEnd
}

Parameters

format - the HexFormat to use for formatting, HexFormat.Default by default.

Return the result of formatting this value using the specified format.

Common
JVM
JS
Native
1.9
@ExperimentalStdlibApi fun UInt.toHexString(
    format: HexFormat = HexFormat.Default
): String

(source)

Formats this UInt value using the specified format.

Note that only the HexFormat.upperCase and HexFormat.number properties of the format instance affect the formatting result of this numeric value.

This function converts the UInt value into its hexadecimal representation by mapping each four-bit chunk of its binary representation to the corresponding hexadecimal digit, starting from the most significant bits. The HexFormat.upperCase property determines whether upper-case (0-9, A-F) or lower-case (0-9, a-f) hexadecimal digits are used for this conversion. The HexFormat.number property adjusts the length of the hexadecimal representation by adding or removing leading zeros as needed, and specifies the strings that prefix and suffix the resulting representation.

Refer to HexFormat.NumberHexFormat for details about the available format options and their impact on formatting.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
// Using the default format
val value = 58
println(value.toHexString()) // 0000003a

// Converts the Int to an unsigned hexadecimal representation
println((-1).toHexString()) // ffffffff

// Using a custom format
val format = HexFormat {
    upperCase = true
    number {
        prefix = "#"
        removeLeadingZeros = true
        minLength = 6
    }
}
println(value.toHexString(format)) // #00003A

// Formatting an unsigned value with the same binary representations yields the same string
val uValue = 58u
println(uValue.toHexString(format)) // #00003A
//sampleEnd
}

Parameters

format - the HexFormat to use for formatting, HexFormat.Default by default.

Return the result of formatting this value using the specified format.

Common
JVM
JS
Native
1.9
@ExperimentalStdlibApi fun ULong.toHexString(
    format: HexFormat = HexFormat.Default
): String

(source)

Formats this ULong value using the specified format.

Note that only the HexFormat.upperCase and HexFormat.number properties of the format instance affect the formatting result of this numeric value.

This function converts the ULong value into its hexadecimal representation by mapping each four-bit chunk of its binary representation to the corresponding hexadecimal digit, starting from the most significant bits. The HexFormat.upperCase property determines whether upper-case (0-9, A-F) or lower-case (0-9, a-f) hexadecimal digits are used for this conversion. The HexFormat.number property adjusts the length of the hexadecimal representation by adding or removing leading zeros as needed, and specifies the strings that prefix and suffix the resulting representation.

Refer to HexFormat.NumberHexFormat for details about the available format options and their impact on formatting.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
// Using the default format
val value = 58L
println(value.toHexString()) // 000000000000003a

// Converts the Long to an unsigned hexadecimal representation
println((-1L).toHexString()) // ffffffffffffffff

// Using a custom format
val format = HexFormat {
    upperCase = true
    number.removeLeadingZeros = true
}
println(value.toHexString(format)) // 3A

// Formatting an unsigned value with the same binary representations yields the same string
val uValue = 58uL
println(uValue.toHexString(format)) // 3A
//sampleEnd
}

Parameters

format - the HexFormat to use for formatting, HexFormat.Default by default.

Return the result of formatting this value using the specified format.