utf8(::Ptr{UInt8}, length::Int = 1)

utf8(::Ptr{UInt8}, [length])

Create a UTF-8 string from the address of a C (0-terminated) string encoded in UTF-8. A copy is made; the ptr can be safely freed. If length is specified, the string does not have to be 0-terminated.

Examples

julia> utf8("hello")
5-element Array{UInt8,1}:
 0x68
 0x65
 0x6c
 0x6c
 0x6f
  1. Convert a string to a UTF-8 byte array:

    julia> utf8("julia")
    5-element Array{UInt8,1}:
    0x6a
    0x75
    0x6c
    0x69
    0x61

    This example converts the string "julia" into a contiguous UTF-8 byte array.

  2. Handle special characters and emojis:

    julia> utf8("🌍")
    4-element Array{UInt8,1}:
    0xf0
    0x9f
    0x8c
    0x8d

    It correctly handles special characters and emojis, converting them to their corresponding UTF-8 representation.

  3. Convert an empty string:
    julia> utf8("")
    0-element Array{UInt8,1}

    When an empty string is provided, it returns an empty UTF-8 byte array.

Common mistake example:

julia> utf8("hello 😊")
ERROR: ArgumentError: invalid UTF-8 sequence of bytes

In this example, the input string contains an invalid UTF-8 sequence of bytes. Make sure that all characters in the string are valid UTF-8 characters to avoid this error.

See Also

ascii, base64decode, Base64DecodePipe, base64encode, Base64EncodePipe, bin, bits, bytestring, charwidth, chomp, chop, chr2ind, contains, endswith, escape_string, graphemes, ind2chr, iscntrl, istext, isupper, isvalid, join, lcfirst, lowercase, lpad, lstrip, normalize_string, num2hex, parseip, randstring, readuntil, replace, repr, rpad, rsplit, rstrip, search, searchindex, split, startswith, string, stringmime, strip, strwidth, summary, takebuf_string, ucfirst, unescape_string, uppercase, utf16, utf32, utf8, wstring,

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: