from Hex(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.
Связанное правило · Use a canonical hexadecimal spelling →Результат
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
СсылкаUse a canonical hexadecimal spelling
Ссылка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
Ссылкаimport std.bytes { Bytes }import std.ioimport std.testio.writeLine(test.assertOk(Bytes.fromHex("")).toHex())io.writeLine(test.assertOk(Bytes.fromHex("000f80ff")).toHex())
000f80ff
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
000f80ff
Reject malformed hexadecimal strings
Ссылкаimport std.bytes { Bytes }import std.iofor text in (["0", "gg", "0x00", "00 11", "00"]) {match (Bytes.fromHex(text)) {case Err(_) => io.writeLine("invalid hex")case Ok(_) => io.writeLine("accepted")}}
invalid hex
invalid hex
invalid hex
invalid hex
invalid hex
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
invalid hexinvalid hexinvalid hexinvalid hexinvalid hex
Reject uppercase hexadecimal input
Ссылкаimport std.bytes { Bytes }import std.iomatch (Bytes.fromHex("CAfe")) {case Err(_) => io.writeLine("uppercase rejected")case Ok(_) => io.writeLine("accepted")}
uppercase rejected
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
uppercase rejected