base64encode(writefunc, args...) base64encode(args...)

Given a write-like function writefunc, which takes an I/O stream as its first argument, base64(writefunc, args...) calls writefunc to write args... to a base64-encoded string, and returns the string. base64(args...) is equivalent to base64(write, args...): it converts its arguments into bytes using the standard write functions and returns the base64-encoded string.


  1. Encode a string to base64 using a custom write function:

    julia> function my_write(stream, data)
              write(stream, uppercase(data))
    julia> base64encode(my_write, "hello world")

    This example defines a custom write-like function my_write that converts the input data to uppercase before writing it to the stream. The base64encode function then uses my_write to encode the string "hello world" to base64.

  2. Encode binary data to base64 using the default write function:

    julia> base64encode([0x01, 0x02, 0x03, 0x04])

    The base64encode function can directly encode binary data, such as an array of bytes, using the default write function.

  3. Encode multiple arguments to base64 using the default write function:
    julia> base64encode(1, "two", 3.0)

    It can encode multiple arguments by converting them to bytes using the default write function and then base64-encoding the resulting bytes.

Common mistake example:

julia> base64encode("hello world")
ERROR: MethodError: no method matching base64encode(::String)

In this example, the mistake is providing a single argument of type String instead of using a write-like function as the first argument. The base64encode function expects a write-like function as the first argument, not a direct input 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.

*Required Field

Checking you are not a robot: