#Assigning arrays and slices of different sizes

1 messages · Page 1 of 1 (latest)

split aspen
#

Im writing a voice chat application to learn zig and i am attempting to get STUN working right now, i want to submit a bind request but im unsure on how to do this right? heres my current code, my issue is on the line where i assign req to BIND_REQUEST_HEAD

const BIND_REQUEST_HEAD = [8]u8{
    0x00, 0x01, // type
    0x00,          0x00, // length
    STUN_MAGIC[0], STUN_MAGIC[1],
    STUN_MAGIC[2], STUN_MAGIC[3],
};

fn bindRequest() ![20]u8 {
    var rng = std.crypto.random;

    var req: [20]u8 = BIND_REQUEST_HEAD;
    for (&req[8..]) |*n| {
        n.* = rng.random();
    }

    return req;
}
clear warren
#

try this?

var req: [20]u8 = undefined;
req[0..8].* = BIND_REQUEST_HEAD;
#

also you probably need a .uintAtMost(u8, 255) after random()