Skip to content

Set decode length in bits #3

@kaimast

Description

@kaimast

The following code to decode a u64 from base64 works fine but is quite ugly:

        let mut array = [0u8; 9];
        base64_url::decode_to_slice(str_rep, &mut array).expect("Not a valid base64 encoding");
        u64::from_le_bytes(array[..8].try_into().unwrap())

The problem here is that the encoded u64 is 11 characters which amounts to 66 bits, not 64. Ideally, I would like to have a decode function that can set the length explicitly or detects it based on the size of the slice.

For example, this would look much nicer:

        let mut array = [0u8; std::mem::size_of::<u64>()];
        base64_url::decode_to_slice_with_length(str_rep, &mut array, u64::BITS).expect("Not a valid base64 encoding");
        u64::from_le_bytes(array)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions