I’m writing two functions hexFromBinary() and binaryFromHex(), and I found that std.zig.parseNumberLiteral() accepts a slice of u8 (bytes: []const u8) to iterate, so I want to reuse the logic or to copy part of that, meaning I might need to convert comptime_int to []const u8, so I chose the @as builtin:
The doc said the operand of @as must be unambiguous, or should I choose u32 or whater for the type of parameter “hex” here, but I would like to let compiler to infer the type because maybe the hex would be really big, say const long_hex_num = F << 20? Anyway, is it possble to convert comptime_int to []const u8 or not?
It seemed that hex is treated as the placeholder of u8 here. I get the idea to convert comptime_int to an array of u8 which will be converted to a slice implictly. But I think the hex, say 0xabcd, would like to be an array .{ '0', 'x', 'a', 'b', 'c', 'd' } because I want to check whether it is a hex number or binary one from the function argument. Maybe we can to get the raw string of that comptime_int and then convert it to an array of u8?
Hex isn’t a different kind of number, it is a visual format for representing number values; integers in Zig, and every other language, are only values not visual formats.
it doesn’t matter if it’s a comptime_int or an explicitly sized integer, you don’t get any info about the format the programmer wrote it in. You don’t even know if it was written manually or the result of some math or logic.
If you want text you should ask for text, that is a []const u8. Zig doesn’t have a string/text type for the same reason Zig doesn’t have a hex/decimal integer types, there are many formats/encodings; though Zig only supports one for what programmers can manually write.
I should specify the format in a Io.Writer.print(), like {X}, {b} to get the different format of the same integer value. As for binaryFromHex(), it should be a wrapper of that formatting function. That makes sense, and since format are not encoded as integer type information, I should rename these two functions into toFmtBinary(num: comptime_int) and toFmtHex(num: comptime_int) no matter paramater num is of format hex or whatever. Yeah it should be that. Thanks vulpesx!