to Base64() - > string- Модуль
- std.
bytes → - Получатель
- Bytes
- Минимальное число аргументов
- 0
- Максимальное число аргументов
- 0
Declaration and parameters
СсылкаДоступность
Включено по умолчанию · Не определено
Import std.bytes in language mode 5.21.
The current compiler selects std/bytes.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.
Получатель
Bytes
Ограничения параметров
Bytes.toBase64 uses the RFC 4648 standard alphabet with plus and slash. Its output has no whitespace or line wrapping.
A final group containing one byte ends with two equals signs. A final group containing two bytes ends with one equals sign.
Bytes.fromBase64 accepts complete groups of four characters. Padding appears at the end of the final group and all unused bits are zero.
Missing or misplaced padding, nonzero unused bits, whitespace, non-ASCII characters, minus signs, and underscores return Err with a string.
Empty bytes encode as empty text and empty text decodes to Ok with empty Bytes. Invalid text never returns a partial byte sequence.
The input is evaluated once and is unchanged. Encoding then decoding recovers the input bytes exactly without any external effects.
Связанное правило · Encode and validate padded Base64 →Результат
The declared result type is string.
Эффекты
Ошибки
Result and effects
СсылкаEmit standard RFC 4648 Base64 with required equals-sign padding and no line wrapping.
Resolved contracts
СсылкаRelated entries
СсылкаEncode and validate padded Base64
СсылкаEncode and validate padded Base64
Bytes.toBase64 uses the RFC 4648 standard alphabet with plus and slash. Its output has no whitespace or line wrapping.
A final group containing one byte ends with two equals signs. A final group containing two bytes ends with one equals sign.
Bytes.fromBase64 accepts complete groups of four characters. Padding appears at the end of the final group and all unused bits are zero.
Missing or misplaced padding, nonzero unused bits, whitespace, non-ASCII characters, minus signs, and underscores return Err with a string.
Empty bytes encode as empty text and empty text decodes to Ok with empty Bytes. Invalid text never returns a partial byte sequence.
The input is evaluated once and is unchanged. Encoding then decoding recovers the input bytes exactly without any external effects.
Причина
A precise alphabet and padding rule let independent implementations exchange the same bytes. Rejecting nonzero unused bits prevents several texts from representing one accepted encoding.
Use standard Base64 vectors
Ссылкаimport std.bytes { Bytes }import std.ioimport std.testfor text in (["", "f", "fo", "foo"]) {io.writeLine(text.encodeUtf8().toBase64())}for text in (["", "Zg==", "Zm8=", "Zm9v"]) {io.writeLine(test.assertOk(Bytes.fromBase64(text)).toHex())}
Zg==
Zm8=
Zm9v
66
666f
666f6f
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
Zg==Zm8=Zm9v66666f666f6f
Reject padding and alphabet errors
Ссылкаimport std.bytes { Bytes }import std.iofor text in (["Zg", "Zh==", "Zg== ", "_w==", "Zg==Zg==", "=g=="]) {match (Bytes.fromBase64(text)) {case Err(_) => io.writeLine("invalid Base64")case Ok(_) => io.writeLine("accepted")}}
invalid Base64
invalid Base64
invalid Base64
invalid Base64
invalid Base64
invalid Base64
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
invalid Base64invalid Base64invalid Base64invalid Base64invalid Base64invalid Base64