from Hex(s: string) - > Result<Bytes, string>- 수신자
- 해당 없음
- 최소 인자 수
- 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