#integer type to []const u8 (ascii string)

1 messages · Page 1 of 1 (latest)

dawn mural
#

well you can bitcast that u32 into a [4]u8, then getting the reference of the resulting var should coerce to a slice, but watch out for endian-ness and lifetime problems

south aurora
#

@as only does type coercion when it is an explicit safe conversion that the compiler knows is always ok. For other stuff you need @bitCast or @ptrCast

olive sundial
#

basically you can only do @as(T, foo) when you could also instead do const bar: T = foo;

south aurora
#

yeah, it probably meant that you need to use @as([4]u8, @bitCast(myInt)). It's a bit of an odd way of doing things, but it allows type inference in a lot of cases. Casts used to take the type to cast to as a parameter, but now you are supposed to use @as if you need to specify the type.