Hexadecimal v0.1.0
HexCase
pub enum HexCase {
Lower,
Upper
}
Which case hexadecimal digits above nine are written in. Copy, carrying no payload. Decoding accepts both regardless.
Cases
| Name | Description |
|---|---|
Lower | a through f, the form most text formats and every cryptographic test vector use. |
Upper | A through F, the form RFC 4648 prints its alphabet in. |
HexEncodedLength
pub func HexEncodedLength(length: uint) -> uint
How many characters the hexadecimal form of length bytes occupies: exactly twice as many.
Cannot fail for any length a slice in memory can have. Runs in constant time and allocates nothing.
Parameters
| Name | Type | Description |
|---|---|---|
length | uint | the number of bytes to encode |
Returns: the number of characters HexEncode writes
HexEncode
pub func HexEncode(
bytes: byte[..],
into: var char8[..],
letterCase: HexCase
) -> uint ! FormatError
pub func HexEncode(
text: char8[..],
into: var char8[..],
letterCase: HexCase
) -> uint ! FormatError
Writes bytes as hexadecimal digits into into, two per byte, high nibble first.
Runs in time that depends only on the length of bytes. Reports InsufficientCapacity when into is shorter
than HexEncodedLength(bytes.length), leaving it exactly as it was; room past the encoded form is not touched.
| Overload | Summary |
|---|---|
pub func HexEncode(bytes: byte[..], into: var char8[..], letterCase: HexCase) -> uint ! FormatError | Writes bytes as hexadecimal digits into into, two per byte, high nibble first. |
pub func HexEncode(text: char8[..], into: var char8[..], letterCase: HexCase) -> uint ! FormatError | Writes the bytes of text as hexadecimal digits into into, two per byte. |
Parameters
| Name | Type | Description |
|---|---|---|
bytes | byte[..] | the bytes to encode |
into | var char8[..] | the destination, at least HexEncodedLength(bytes.length) characters |
letterCase | HexCase | which case the digits above nine are written in |
text | char8[..] | the text whose bytes to encode |
Returns: the number of characters written
HexDecodedLength
pub func HexDecodedLength(text: char8[..]) -> uint ! DecodeError
How many bytes the hexadecimal text decodes to, after checking that its length is even.
Only the length is checked; the digits themselves are checked by HexDecode. Reports InvalidLength at the
end of the text when it has an odd number of characters. Runs in constant time and allocates nothing.
Parameters
| Name | Type | Description |
|---|---|---|
text | char8[..] | the hexadecimal text |
Returns: the number of bytes HexDecode writes for it
HexDecode
pub func HexDecode(
text: char8[..],
into: var byte[..]
) -> uint ! DecodeError
Reads hexadecimal digits of either case from text into into, one byte per pair.
Runs in time that depends only on the length of text when the text is valid. Reports InvalidLength for an
odd number of characters and InvalidCharacter at the first character that is not a hexadecimal digit, leaving
into untouched in both cases, because the whole text is checked before anything is written. Reports
InsufficientCapacity with the length needed when into is too short, also leaving it untouched.
Parameters
| Name | Type | Description |
|---|---|---|
text | char8[..] | the hexadecimal text to decode |
into | var byte[..] | the destination, at least HexDecodedLength(text) bytes |
Returns: the number of bytes written
HexView
pub struct HexView {
// private fields
}
A borrowed byte sequence that renders as hexadecimal under a format placeholder.
Lets a caller write Render("{}", ViewHex(digest)) instead of sizing a buffer first. The view borrows the bytes
and must not outlive them. Copy, owning nothing.
Implements Display
HexView conforms to Display.
WriteDisplay
pub func WriteDisplay(
self: &HexView,
writer: &var TextWriter,
spec: FormatSpec
) -> ! FormatError
Writes the bytes as hexadecimal digits, padded to the placeholder's width.
The style x selects lower case and X upper case; with neither, the view's own case is used. The digits
are produced in blocks, so rendering allocates nothing however long the bytes are. Reports whatever the writer
reported, and UnsupportedRequest for a precision or any other style.
| Name | Type | Description |
|---|---|---|
writer | &var TextWriter | the destination |
spec | FormatSpec | what the placeholder asked for |
Returns: success when the whole rendering was accepted
ViewHex
pub func ViewHex(
bytes: byte[..],
letterCase: HexCase
) -> HexView
Borrows bytes for rendering as hexadecimal.
Cannot fail and allocates nothing; the view reads bytes each time it is rendered, so it must not outlive them.
Parameters
| Name | Type | Description |
|---|---|---|
bytes | byte[..] | the bytes to render |
letterCase | HexCase | the case used when the placeholder does not name one |
Returns: a view that renders bytes as hexadecimal