I’m following the Zig documentation trying to print some Unicode emoji characters but I’m on a windows terminal. I know that Zig is using UTF - 8 but windows is using UTF - 16, is there a way to get those two to mingle so that I can print on the windows terminal? Right now i just get garbage out put of random symbols.
It is worse… Windows use OEM code pages for console applications (for US OEM is 437, the original DOS code page).
You can set the code page to utf8 from the terminal by running chcp 65001
.
Programmatically you can set the output to utf-8 by calling SetConsoleOutputCP
(See: The Incredible Unicode Mess - #31 by squeek502)
That is a mess, maybe I’ll just move to Linux after all lol. I appreciate the quick response.
in the example it works, but this also works
const std = @import("std");
const builtin = @import("builtin");
pub fn main() !void {
if (builtin.os.tag == .windows) {
_ = std.os.windows.kernel32.SetConsoleOutputCP(65001);
}
std.debug.print("\u{00a9}", .{});
}
is there any problem with doing it this way?
The scope of the code page is the console/terminal.
If someone launches your application from GUI there is no problem because the console/terminal closes after your application exits.
If someone launch a terminal first and then launch your application from the terminal shell, after your application exits the code page remains in utf8. This might be or might not be a problem for the applications that follow until the terminal is closed.
so deinit to make sure it doesnt persist, got it