hex2bytes

hex2bytes(s::ASCIIString)

Convert an arbitrarily long hexadecimal string to its binary representation. Returns an Array{UInt8,1}, i.e. an array of bytes.

Examples

julia> hex2bytes("48656c6c6f20576f726c64")
13-element Array{UInt8,1}:
 0x48
 0x65
 0x6c
 0x6c
 0x6f
 0x20
 0x57
 0x6f
 0x72
 0x6c
 0x64
  1. Convert hexadecimal string to byte array:

    julia> hex2bytes("48656c6c6f20576f726c64")
    13-element Array{UInt8,1}:
    0x48
    0x65
    0x6c
    0x6c
    0x6f
    0x20
    0x57
    0x6f
    0x72
    0x6c
    0x64

    This example converts the hexadecimal string "48656c6c6f20576f726c64" to an array of bytes.

  2. Handle empty string input:

    julia> hex2bytes("")
    0-element Array{UInt8,1}

    It correctly handles the case where an empty string is provided as input.

  3. Convert shorter hexadecimal strings:
    julia> hex2bytes("ab12")
    2-element Array{UInt8,1}:
    0xab
    0x12

    It can convert shorter hexadecimal strings as well.

Common mistake example:

julia> hex2bytes("48656c6c6f20 576f726c64")
ERROR: ArgumentError: Invalid hexadecimal string: contains invalid characters or spaces

In this example, the input string contains invalid characters or spaces, resulting in an ArgumentError. hex2bytes expects a valid hexadecimal string without any spaces or other special characters. Make sure the input string consists only of valid hexadecimal characters.

See Also

base, big, bytes2hex, cconvert, complex, convert, dec, hex, hex2bytes, hex2num, oct, oftype, parse, promote, signed, unsafe_convert, unsigned, widen,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: