Перейти к содержимому
Топаз6.0 · До релиза
Загрузка →

Поиск в документации

Спецификация 6.0

Выберите результат стрелками и нажмите Enter, чтобы открыть его.

Введите имя API, номер диагностики, правило или тему проекта.

    Библиотека · std.bytes

    Bytes.fromHex

    Decodes hexadecimal text into Bytes.

    fromHex(s: string) -> Result<Bytes, string>
    Модуль
    std.bytes →
    Получатель
    Неприменимо
    Минимальное число аргументов
    1
    Максимальное число аргументов
    1

    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.toHex emits two lowercase ASCII hexadecimal digits per byte with the high nibble first. Empty Bytes produces an empty string.

    Bytes.fromHex accepts an even number of digits drawn from 0 through 9 and a through f. Empty text returns Ok with empty Bytes.

    Uppercase letters, prefixes, separators, whitespace, non-ASCII characters, and an odd digit count return Err with a string describing invalid hex input.

    Decoding returns the entire byte sequence in Ok or an error with no decoded prefix. It never mutates an input value.

    Encoding then decoding recovers the bytes exactly. Decoding then encoding preserves every accepted input spelling.

    The input expression evaluates once. These conversions perform no I/O and do not apply Unicode normalization.

    Результат

    The declared result type is Result<Bytes, string>.

    Эффекты

    Ошибки

    Result and effects

    Ссылка

    Decode an even number of lowercase hexadecimal digits. Invalid characters or odd length return Err with a string.

    Resolved contracts

    Ссылка

    Related entries

    Ссылка

    std.bytes →

    Use a canonical hexadecimal spelling

    Ссылка
    Правилоtopaz.stdlib.contract.bytes-hex.rule

    Use a canonical hexadecimal spelling

    Bytes.toHex emits two lowercase ASCII hexadecimal digits per byte with the high nibble first. Empty Bytes produces an empty string.

    Bytes.fromHex accepts an even number of digits drawn from 0 through 9 and a through f. Empty text returns Ok with empty Bytes.

    Uppercase letters, prefixes, separators, whitespace, non-ASCII characters, and an odd digit count return Err with a string describing invalid hex input.

    Decoding returns the entire byte sequence in Ok or an error with no decoded prefix. It never mutates an input value.

    Encoding then decoding recovers the bytes exactly. Decoding then encoding preserves every accepted input spelling.

    The input expression evaluates once. These conversions perform no I/O and do not apply Unicode normalization.

    Причина

    One accepted hexadecimal spelling makes serialized byte text stable for comparison and reproduction. Data that uses another spelling can be normalized explicitly before decoding.

    Decode byte pairs and emit lowercase text

    Ссылка
    main.tpzDecode byte pairs and emit lowercase textОткрыть песочницу →
    1. import std.bytes { Bytes }
    2. import std.io
    3. import std.test
    4. io.writeLine(test.assertOk(Bytes.fromHex("")).toHex())
    5. io.writeLine(test.assertOk(Bytes.fromHex("000f80ff")).toHex())
    Ожидаемый выводКод завершения 0
    
    000f80ff
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Выполнение завершается. Код завершения 0.

    Ожидаемый стандартный вывод
    1. 000f80ff

    Reject malformed hexadecimal strings

    Ссылка
    main.tpzReject malformed hexadecimal stringsОткрыть песочницу →
    1. import std.bytes { Bytes }
    2. import std.io
    3. for text in (["0", "gg", "0x00", "00 11", "00"]) {
    4. match (Bytes.fromHex(text)) {
    5. case Err(_) => io.writeLine("invalid hex")
    6. case Ok(_) => io.writeLine("accepted")
    7. }
    8. }
    Ожидаемый выводКод завершения 0
    invalid hex
    invalid hex
    invalid hex
    invalid hex
    invalid hex
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Выполнение завершается. Код завершения 0.

    Ожидаемый стандартный вывод
    1. invalid hex
    2. invalid hex
    3. invalid hex
    4. invalid hex
    5. invalid hex

    Reject uppercase hexadecimal input

    Ссылка
    main.tpzReject uppercase hexadecimal inputОткрыть песочницу →
    1. import std.bytes { Bytes }
    2. import std.io
    3. match (Bytes.fromHex("CAfe")) {
    4. case Err(_) => io.writeLine("uppercase rejected")
    5. case Ok(_) => io.writeLine("accepted")
    6. }
    Ожидаемый выводКод завершения 0
    uppercase rejected
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Выполнение завершается. Код завершения 0.

    Ожидаемый стандартный вывод
    1. uppercase rejected

    Меню

    Загрузка →