# Convert comptime\_int to a slice of u8

**URL:** <https://ziggit.dev/t/convert-comptime-int-to-a-slice-of-u8/17743>\
**Category:** Help\
**Tags:** language\
**Created:** [September 28, 2026, 1:24pm UTC](https://ziggit.dev/t/convert-comptime-int-to-a-slice-of-u8/17743 "2026-09-28T13:24:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeeqeeng](https://ziggit.dev/letter_avatar_proxy/v4/letter/z/8baadc/32.png) [@zeeqeeng](https://ziggit.dev/u/zeeqeeng)\
**Post date:** [September 28, 2026, 1:24pm UTC](https://ziggit.dev/t/convert-comptime-int-to-a-slice-of-u8/17743/1 "2026-09-28T13:24:06Z")

</div>

I’m writing two functions `hexFromBinary()` and `binaryFromHex()`, and I found that [`std.zig.parseNumberLiteral()`](https://codeberg.org/ziglang/zig/src/commit/b6896c43001ea3ed8e5307fadcf58ff04255bc70/lib/std/zig/number_literal.zig#L63) 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:

```zig
fn binaryFromHex(hex: comptime_int) !u32 {
    const bytes = @as([]const u8, hex);
    _ = bytes;
}

```

but it seemed it’s not the expected usage of `@as`.

```zig
expected type '[]const u8', found 'comptime_int'

```

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?

---

<div class="post-metadata">

**Author:** ![vulpesx](https://ziggit.dev/user_avatar/ziggit.dev/vulpesx/32/3989_2.png) [@vulpesx](https://ziggit.dev/u/vulpesx)\
**Post date:** [September 28, 2026, 1:43pm UTC](https://ziggit.dev/t/convert-comptime-int-to-a-slice-of-u8/17743/2 "2026-09-28T13:43:54Z")

</div>

`@as` only does coercion, in other words, it only does what you can do with `const b: T = a;`

fortunately `comptime_int` does coerce to `u8`, so you can just do `&[_]u8{hex}` (which is actually a pointer to an array, but that will coerce to a slice)

---

<div class="post-metadata">

**Author:** ![zeeqeeng](https://ziggit.dev/letter_avatar_proxy/v4/letter/z/8baadc/32.png) [@zeeqeeng](https://ziggit.dev/u/zeeqeeng)\
**Post date:** [September 28, 2026, 1:55pm UTC](https://ziggit.dev/t/convert-comptime-int-to-a-slice-of-u8/17743/3 "2026-09-28T13:55:39Z")

</div>

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`?

---

<div class="post-metadata">

**Author:** ![vulpesx](https://ziggit.dev/user_avatar/ziggit.dev/vulpesx/32/3989_2.png) [@vulpesx](https://ziggit.dev/u/vulpesx)\
**Post date:** [September 28, 2026, 2:07pm UTC](https://ziggit.dev/t/convert-comptime-int-to-a-slice-of-u8/17743/4 "2026-09-28T14:07:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zeeqeeng](https://ziggit.dev/letter_avatar_proxy/v4/letter/z/8baadc/32.png) [@zeeqeeng](https://ziggit.dev/u/zeeqeeng)\
**Post date:** [September 28, 2026, 2:16pm UTC](https://ziggit.dev/t/convert-comptime-int-to-a-slice-of-u8/17743/5 "2026-09-28T14:16:06Z")

</div>

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!
