solve Challenge 1 of Set 1

master
Thomas Lindner 2022-02-06 16:15:18 +01:00
commit f042ca64b5
3 changed files with 58 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.*.swp
*/*/zig-cache
*/*/zig-out

View File

@ -0,0 +1,27 @@
const std = @import("std");
pub fn build(b: *std.build.Builder) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
const exe = b.addExecutable("challenge1_hex2base64", "src/main.zig");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.install();
const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}

View File

@ -0,0 +1,27 @@
const std = @import("std");
pub fn main() anyerror!void {
const stdin = std.io.bufferedReader(std.io.getStdIn().reader()).reader();
const stdout = std.io.getStdOut().writer();
var inputbuffer = std.mem.zeroes([1024]u8);
var rawbuffer = std.mem.zeroes([512]u8);
var outputbuffer = std.mem.zeroes([1024]u8);
while (true) {
const line = stdin.readUntilDelimiterOrEof(inputbuffer[0..], '\n') catch |err| {
std.log.err("input failed: {s}\n", .{err});
return;
};
const hex = line orelse return;
const raw = std.fmt.hexToBytes(rawbuffer[0..], hex) catch {
std.log.err("not a valid hexadecimal\n", .{});
continue;
};
const base64 = std.base64.standard.Encoder.encode(outputbuffer[0..], raw);
stdout.print("{s}\n", .{base64}) catch |err| {
std.log.err("output failed: {s}\n", .{err});
return;
};
}
}