to Base64() - > string- 수신자
- 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