Character codes: Difference between revisions

→‎{{header|Zig}}: update to use main() and stdout
No edit summary
(→‎{{header|Zig}}: update to use main() and stdout)
Line 2,753:
=={{header|Zig}}==
<syntaxhighlight lang="zig">const std = @import("std");
 
const debug = std.debug;
const unicode = std.unicode;
 
pub fn main() !void {
test "character codes" {
const stdout = std.io.getStdOut().writer();
debug.warn("\n", .{});
 
try characterAsciiCodes(stdout);
try characterUnicodeCodes(stdout);
}
 
fn characterAsciiCodes(writer: anytype) !void {
try writer.writeAll("Sample ASCII characters and codes:\n");
 
// Zig's string is just an array of bytes (u8).
const message: []const u8 = "ABCabc";
 
for (message) |val| {
debugtry writer.warnprint(" '{c}' code: {d} [hexa: 0x{x}]\n", .{ val, val, val });
}
try writer.writeByte('\n');
}
 
fn characterUnicodeCodes(writer: anytype) !void {
test "character (uni)codes" {
try writer.writeAll("Sample Unicode characters and codes:\n");
debug.warn("\n", .{});
 
const message: []const u8 = "あいうえお";
 
const utf8_view = unicode.Utf8View.initUnchecked(message);
Line 2,778 ⟶ 2,784:
while (iter.nextCodepoint()) |val| {
var array: [4]u8 = undefined;
varconst slice = array[0..try unicode.utf8Encode(val, &array)];
 
debugtry writer.warnprint(" '{s}' code: {d} [hexa: U+{x}]\n", .{ slice, val, val });
}
try writer.writeByte('\n');
}</syntaxhighlight>
 
{{out}}
<pre>TestSample [1/2]ASCII testcharacters "characterand codes"...:
'A' code: 65 [hexa: 0x41]
'B' code: 66 [hexa: 0x42]
Line 2,792 ⟶ 2,799:
'b' code: 98 [hexa: 0x62]
'c' code: 99 [hexa: 0x63]
 
Test [2/2] test "character (uni)codes"...
Sample Unicode characters and codes:
'あ' code: 12354 [hexa: U+3042]
'い' code: 12356 [hexa: U+3044]
'う' code: 12358 [hexa: U+3046]
'え' code: 12360 [hexa: U+3048]
'お' code: 12362 [hexa: U+304a]</pre>
 
All 2 tests passed.</pre>
=={{header|zkl}}==
The character set is 8 bit ASCII (but doesn't care if you use UTF-8 or unicode characters).
59

edits