#error - crash

1 messages · Page 1 of 1 (latest)

ashen pulsar
#
 while (true) {
            const received_bytes = try std.posix.recvfrom(socket, buffer[0..], 0, null, null);
            try streams.reset();
            try streams.buff.appendSlice(std.mem.bytesAsSlice(u8, buffer[0..received_bytes]));
            std.debug.print("Received {d} bytes: {s}\n", .{ received_bytes, buffer[0..received_bytes] });
            _ = try streams.readByte();
            const time = streams.readInt64B();
            std.debug.print("raknet time is {!}\n", .{time});
            var i: usize = 0;
            var MAGIC: []u8 = undefined;
            while (i < 16) : (i += 1) {
                MAGIC[i] = try streams.readByte2();
            }
            std.debug.print("raknet magic is {x}\n", .{MAGIC});
        }

It crashes in the magic line, do you have a solution, I am trying to convert it to HEX

whole shuttle
#

it would be wrong to assign a slice to undefined in virtually every case

#

since a slice is const Slice = struct { ptr: [*]Type, len: usize }

#

if you know that there are always going to be 16 elements in MAGIC, you can do var MAGIC: [16]u8 = undefined

#

otherwise, you'll need to allocate the number of bytes you want

ashen pulsar
whole shuttle
#

try std.fmt.fmtSliceHexLower and std.fmt.fmtSliceHexUpper

#

but that won't give you hex immediately, it'll give you a "formatter" which can be passed into functions like std.debug.print to create a hex string there

#

so for example in your debug statement maybe you want

#

std.debug.print("raknet magic is {x}\n", .{ std.fmt.fmtSliceHexUpper(MAGIC) });

#

actually

#

you know what, if MAGIC is always going to be 16 bytes, you can actually just use std.fmt.bytesToHex which does actually return the hex values directly

#

the difference is that fmtSliceHexUpper would need to allocate the hex string, but std.fmt.bytesToHex can return a fixed number of bytes

#

std.debug.print("raknet magic is {s}\n", .{ std.fmt.bytesToHex(MAGIC, .upper) });