我有以下代码:
const std = @import("std");
fn getTLVForValue(writer: anytype, tagNum: []const u8, tagValue: []const u8) !void {
_ = try writer.write(tagNum);;
_ = try writer.writeByte(@intCast(tagValue.len));
_ = try writer.write(tagValue);
}
pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
const allocator = arena.allocator();
var qrCodeBuf = std.ArrayList(u8).init(allocator);
defer qrCodeBuf.deinit();
const writer = qrCodeBuf.writer();
//1. Seller Name
try getTLVForValue(writer, "01", "Bobs Basement Records");
//2. VAT Registration
try getTLVForValue(writer, "02", "312345678901233");
//3. Time stamp
try getTLVForValue(writer, "03", "12-12-2023");
//4. tax Total
try getTLVForValue(writer, "04", "1000");
//5. Vat Total
try getTLVForValue(writer, "05", "115");
//6. Hashed XML
try getTLVForValue(writer, "06", "dsds");
//7. key
try getTLVForValue(writer, "07", "125");
//8. Signature
try getTLVForValue(writer, "08", "dsfsd");
std.debug.print("{any}\n", .{qrCodeBuf.items});
std.debug.print("Hex string: {s}\n", .{std.fmt.fmtSliceHexUpper(qrCodeBuf.items)});
// const hex = try std.fmt.allocPrint("{s}", .{std.fmt.fmtSliceHexUpper(qrCodeBuf.items)});
const output_buffer = try allocator.alloc(u8, std.base64.standard.Encoder.calcSize(qrCodeBuf.items.len));
defer allocator.free(output_buffer);
const data = std.base64.standard.Encoder.encode(output_buffer, qrCodeBuf.items);
std.debug.print("{s}\n", .{data});
const decode_buf = try allocator.alloc(u8, try std.base64.standard.Decoder.calcSizeForSlice(data));
try std.base64.standard.Decoder.decode(decode_buf, data);
std.debug.print("{s}\n", .{decode_buf});
std.debug.print("{}\n", .{std.zig.fmtEscapes(decode_buf)});
try std.testing.expectEqualStrings(qrCodeBuf.items, decode_buf);
}
它给我的最终输出为:
01\x15Bobs Basement Records02\x0f31234567890123303\n12-12-202304\x04100005\x0311506\x04dsds07\x0312508\x05dsfsd
但在这里我得到的是不可打印的 ASCII 字符而不是数字,例如,我得到的\n
不是10
和\x15
而是21
等等,按照此处显示的映射。
我打开了任何替代选项来让函数getTLVForValue
返回TLV - Tag - Length - Value
,这样我就可以得到例如0103xxxx
长度为 is3
且 tagNum 为 的标签01
。
我正在尝试编写一个代码来执行下面屏幕截图中解释的操作: