Do we really have to impl our own function to convert the ascii to its corresponding string represention?
something like this??
const std = @import("std");
pub fn asciiToString(allocator: std.mem.Allocator, ascii_numbers: []const u8) ![]u8 {
var result = try allocator.alloc(u8, ascii_numbers.len);
errdefer allocator.free(result);
for (ascii_numbers, 0..) |num, i| {
if (num < 32 or num > 126) {
return error.InvalidASCII;
}
result[i] = num;
}
return result;
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const ascii_numbers = [_]u8{ 72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33 };
const string = try asciiToString(allocator, &ascii_numbers);
defer allocator.free(string);
std.debug.print("Converted string: {s}\n", .{string});
}
any help is appreciated .