How can I convert a []const u8 to a []const u16?

If you want to allocate on the heap, simplest would just be

const wtf16 = try std.unicode.wtf8ToWtf16LeAlloc(allocator, wtf8);
defer allocator.free(wtf16);

If you want to allocate a buffer, you can get the required size with std.unicode.calcWtf16LeLen (returns the required size for allocating []u16)

If the WTF-8 string is a string literal, you can get a WTF-16 LE version of that with std.unicode.wtf8ToWtf16LeStringLiteral:

const wtf16 = std.unicode.wtf8ToWtf16LeStringLiteral("foo");

Relevant

2 Likes