Function encode [src]
Encodes a binary buffer into a hexadecimal string.
The output buffer must be twice the size of the input buffer.
Prototype
pub fn encode(encoded: []u8, bin: []const u8, comptime case: std.fmt.Case) error{SizeMismatch}!void Parameters
encoded: []u8bin: []const u8case: std.fmt.Case Possible Errors
Source
pub fn encode(encoded: []u8, bin: []const u8, comptime case: std.fmt.Case) error{SizeMismatch}!void {
if (encoded.len / 2 != bin.len) {
return error.SizeMismatch;
}
for (bin, 0..) |v, i| {
const b: u16 = v >> 4;
const c: u16 = v & 0xf;
const off = if (case == .upper) 32 else 0;
const x =
((87 - off + c + (((c -% 10) >> 8) & ~@as(u16, 38 - off))) & 0xff) << 8 |
((87 - off + b + (((b -% 10) >> 8) & ~@as(u16, 38 - off))) & 0xff);
encoded[i * 2] = @truncate(x);
encoded[i * 2 + 1] = @truncate(x >> 8);
}
}