to Hex() - > string- Module
- std.
bytes - Receiver
- Bytes
- Minimum arity
- 0
- Maximum arity
- 0
Declaration and parameters
LinkAvailability
Included by default: Pending
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.
Source evidence
- compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 976 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 52 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 976 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
b48527d0a174· 410 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1296 - compiler/crates/topaz_value/src/value/members.rs at
b48527d0a174· 246
Receiver
Bytes
Parameter constraints
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.
Related rule: Use a canonical hexadecimal spellingResult
The declared result type is string.
Effects
Failures
Result and effects
LinkEmit two lowercase hexadecimal digits per byte with no prefix or separators.
Resolved contracts
LinkRelated entries
LinkUse a canonical hexadecimal spelling
LinkUse 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.
Why
One accepted hexadecimal spelling makes serialized byte text stable for comparison and reproduction. Data that uses another spelling can be normalized explicitly before decoding.
Source evidence
- compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 56 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 123 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 132 -
topaz.stdlib.contract.bytes-hexatworking-tree - topaz.decision.bytes-hex-canonical-input.1 at
topaz.decisi
Decode byte pairs and emit lowercase text
Linkimport std.bytes { Bytes }import std.ioimport std.testio.writeLine(test.assertOk(Bytes.fromHex("")).toHex())io.writeLine(test.assertOk(Bytes.fromHex("000f80ff")).toHex())
000f80ff
Expected result
Expected result
Execution completes. Exit code 0.
000f80ff
Source evidence
- compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 56 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 123 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 132 -
topaz.stdlib.contract.bytes-hexatworking-tree - topaz.decision.bytes-hex-canonical-input.1 at
topaz.decisi
Reject malformed hexadecimal strings
Linkimport 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
Expected result
Expected result
Execution completes. Exit code 0.
invalid hexinvalid hexinvalid hexinvalid hexinvalid hex
Source evidence
- compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 56 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 123 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 132 -
topaz.stdlib.contract.bytes-hexatworking-tree - topaz.decision.bytes-hex-canonical-input.1 at
topaz.decisi
Reject uppercase hexadecimal input
Linkimport std.bytes { Bytes }import std.iomatch (Bytes.fromHex("CAfe")) {case Err(_) => io.writeLine("uppercase rejected")case Ok(_) => io.writeLine("accepted")}
uppercase rejected
Expected result
Expected result
Execution completes. Exit code 0.
uppercase rejected
Source evidence
- compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 56 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 123 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 132 -
topaz.stdlib.contract.bytes-hexatworking-tree - topaz.decision.bytes-hex-canonical-input.1 at
topaz.decisi