bytestring(::Ptr{UInt8},?)
bytestring(::Ptr{UInt8}, [length])
Create a string from the address of a C (0-terminated) string encoded in ASCII or 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> bytestring("Hello")
5-element Array{UInt8,1}:
0x48
0x65
0x6c
0x6c
0x6f
-
Convert string to byte array:
julia> bytestring("Julia") 5-element Array{UInt8,1}: 0x4a 0x75 0x6c 0x69 0x61
This example converts the string "Julia" into a byte array representation.
-
Handle UTF-8 encoded strings:
julia> bytestring("안녕하세요") 15-element Array{UInt8,1}: 0xec 0x95 0x88 0xeb 0x85 0x95 0xed 0x95 0x98 0xec 0x84 0xb8 0xec 0x9a 0x94
The
bytestring
function handles UTF-8 encoded strings correctly, producing the corresponding byte array. - Convert ASCII string to byte array:
julia> bytestring("ASCII") 5-element Array{UInt8,1}: 0x41 0x53 0x43 0x49 0x49
It can also convert ASCII strings into byte arrays.
Common mistake example:
julia> bytestring(123)
ERROR: MethodError: no method matching bytestring(::Int64)
In this example, an integer argument is passed to the bytestring
function. It's important to note that bytestring
is meant to work with string inputs and not other data types. Ensure that the argument passed to bytestring
is a string.
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.