Encoding.fromHex
func Encoding.fromHex(s: string): Result[Bytes, Error]Encoding.fromHex decodes s, hex digits of either case, into bytes. It is Err on an odd length or a character that is not a hex digit.
Details
- Upper-case and lower-case digits decode alike, and may be mixed.
- Every character must be a hex digit: a
0xprefix, spaces, line breaks, and separators such as:make the resultErr. Remove them first.
Basic examples
Encoding.fromHex("6869") // Ok{ value: Bytes(68 69) }
Encoding.fromHex("DEADbeef") // Ok{ value: Bytes(de ad be ef) }
Encoding.fromHex("abc") // Err{ error: Error{ message: "odd length hex string" } }Scope
The empty string decodes to empty bytes:
Encoding.fromHex("") // Ok{ value: Bytes() }A character that is not a hex digit is an error:
Encoding.fromHex("1g") // Err{ error: Error{ message: "invalid byte: U+0067 'g'" } }A 0x prefix, a space, and a letter other than a to f are all errors:
Encoding.fromHex("0x12").isOk() // false
Encoding.fromHex("12 34").isOk() // false
Encoding.fromHex("é0").isOk() // falseTwo digits make one byte, so the result has half as many bytes as the string has digits:
let b = Encoding.fromHex("00ff7f") | Result.unwrap()
b.length() // 3
b[1] // 255
b[2] // 127The parameter is named s:
Encoding.fromHex(s: "ff") // Ok{ value: Bytes(ff) }In a pipe, the string is the first argument:
"cafe" | Encoding.fromHex() | Result.map({ x.length() }) // Ok{ value: 2 }Applications
Check a digest
Compare data's SHA-256 with a hex digest published next to it, such as a download's checksum. Decode the published digest and compare bytes, not strings, so that the case of its digits does not matter:
func verify(data: Bytes, digest: string): Result[bool, Error] {
let want = try (Encoding.fromHex(digest) | Result.wrapErr("digest"))
Result.ok(Crypto.equal(Crypto.sha256(data), want))
}
let data = "hello".toBytes()
let published = "2CF24DBA5FB0A30E26E83B2AC5B9E29E1B161E5C1FA7425E73043362938B9824"
println(verify(data, published))
println(verify(data, "2cf2"))
println(verify(data, "not hex"))Ok{ value: true }
Ok{ value: false }
Err{ error: Error{ message: "digest: invalid byte: U+006E 'n'" } }Parse a color
Read a CSS color such as #ff8800 into its three channels:
type Rgb = { r: byte, g: byte, b: byte }
func parseColor(s: string): Result[Rgb, Error] {
if !s.startsWith("#") || s.length() != 7 {
return Result.fail("color ${s}: want #rrggbb")
}
let bytes = try (Encoding.fromHex(s.trimPrefix("#")) | Result.wrapErr("color ${s}"))
Result.ok(Rgb{ r: bytes[0], g: bytes[1], b: bytes[2] })
}
println(parseColor("#ff8800"))
println(parseColor("#FFF"))
println(parseColor("#12345z"))Ok{ value: Rgb{ r: 255, g: 136, b: 0 } }
Err{ error: Error{ message: "color #FFF: want #rrggbb" } }
Err{ error: Error{ message: "color #12345z: invalid byte: U+007A 'z'" } }Hex with separators
A MAC address writes its bytes with : between them. Remove the separators, then decode:
let mac = "00:1A:2b:3C:4d:5E"
let b = mac.replace(":", "") | Encoding.fromHex() | Result.unwrap()
println(b.length())
println(Encoding.toHex(b))6
001a2b3c4d5eHex-encoded text
Decode hex to bytes, then the bytes to a string with String.fromBytes:
Encoding.fromHex("416e74") | Result.unwrap() | String.fromBytes() // "Ant"Properties and relations
Encoding.toHex writes lower-case digits, so a round trip gives back the string in lower case:
for s in ["", "00", "deadbeef", "DEADBEEF", "0aFf"] {
assert((Encoding.fromHex(s) | Result.unwrap() | Encoding.toHex()) == s.toLower())
}Decoding what Encoding.toHex wrote gives back the same bytes:
for b in ["", "ant", "🐜"].map(String.toBytes) {
assert((Encoding.toHex(b) | Encoding.fromHex() | Result.unwrap()) == b)
}A string of an odd length is always Err, even when every character is a digit:
for s in ["0", "abc", "12345"] {
assert(!Encoding.fromHex(s).isOk())
}Possible issues
The result is a Result, not Bytes. Unwrap it, or use try in a function that returns a Result:
let b: Bytes = Encoding.fromHex("ff")
// error: cannot use Result[Bytes, Error] where Bytes is expectedA leading zero matters. A number written in hex without it, such as f, has an odd length; pad it to an even length first:
Encoding.fromHex("f").isOk() // false
Encoding.fromHex("f".padStart(2, pad: "0")) // Ok{ value: Bytes(0f) }A 0x prefix and surrounding whitespace are not hex digits. Trim them before decoding:
Encoding.fromHex(" 0x1f\n").isOk() // false
Encoding.fromHex(" 0x1f\n".trim().trimPrefix("0x")) // Ok{ value: Bytes(1f) }See also
Encoding.toHex, Encoding.fromBase64, Encoding, String.fromBytes, Bytes.fromArray, Crypto.sha256, Crypto.equal
