본문으로 건너뛰기
토파즈6.0 · 출시 전
다운로드 →

문서 검색

명세 6.0

방향키로 결과를 고르고 Enter를 눌러 여세요.

API 이름, 진단 번호, 규칙 또는 프로젝트 주제를 입력하세요.

    라이브러리 · std.bytes

    Bytes.toBase64

    Encodes this byte sequence as Base64 text.

    toBase64() -> 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.

    결과

    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

    링크

    std.bytes →

    Encode and validate padded Base64

    링크
    규칙topaz.stdlib.contract.bytes-base64.rule

    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

    링크
    main.tpzUse standard Base64 vectors플레이그라운드 열기 →
    1. import std.bytes { Bytes }
    2. import std.io
    3. import std.test
    4. for text in (["", "f", "fo", "foo"]) {
    5. io.writeLine(text.encodeUtf8().toBase64())
    6. }
    7. for text in (["", "Zg==", "Zm8=", "Zm9v"]) {
    8. io.writeLine(test.assertOk(Bytes.fromBase64(text)).toHex())
    9. }
    기대 출력종료 코드 0
    
    Zg==
    Zm8=
    Zm9v
    
    66
    666f
    666f6f
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. Zg==
    2. Zm8=
    3. Zm9v
    4. 66
    5. 666f
    6. 666f6f

    Reject padding and alphabet errors

    링크
    main.tpzReject padding and alphabet errors플레이그라운드 열기 →
    1. import std.bytes { Bytes }
    2. import std.io
    3. for text in (["Zg", "Zh==", "Zg== ", "_w==", "Zg==Zg==", "=g=="]) {
    4. match (Bytes.fromBase64(text)) {
    5. case Err(_) => io.writeLine("invalid Base64")
    6. case Ok(_) => io.writeLine("accepted")
    7. }
    8. }
    기대 출력종료 코드 0
    invalid Base64
    invalid Base64
    invalid Base64
    invalid Base64
    invalid Base64
    invalid Base64
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. invalid Base64
    2. invalid Base64
    3. invalid Base64
    4. invalid Base64
    5. invalid Base64
    6. invalid Base64

    메뉴

    다운로드 →