ascii(::Ptr{UInt8},?)
ascii(::Ptr{UInt8}, [length])
Create an ASCII string from the address of a C (0-terminated) string encoded in ASCII. A copy is made; the ptr can be safely freed. If length
is specified, the string does not have to be 0-terminated.
Examples
-
Convert a string to ASCII:
julia> ascii("Hello World!") "Hello World!"
The
ascii
function converts a string to a contiguous ASCII string. In this example, the input string "Hello World!" is already a valid ASCII string, so it remains unchanged. -
Handle non-ASCII characters:
julia> ascii("こんにちは") "\\u3053\\u3093\\u306B\\u3061\\u306F"
If the input string contains non-ASCII characters, the
ascii
function converts them to their Unicode escape sequence representation. In this example, the Japanese greeting "こんにちは" is converted to "\u3053\u3093\u306B\u3061\u306F". - Keep non-ASCII characters as-is:
julia> ascii("Café") "Caf\\xe9"
The
ascii
function preserves the non-ASCII characters as-is, without converting them to escape sequences. In this example, the string "Café" is converted to "Caf\xe9".
Common mistake example:
julia> ascii("你好")
ERROR: ArgumentError: invalid character for ASCII string
In this example, the input string contains non-ASCII characters that are not valid ASCII characters. The ascii
function expects all characters in the string to be valid ASCII characters.
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.