#base64-url #integer #string #16-bit #encoded-string #64-bit

no-std base64id

Efficiently represent 64, 32 and 16 bit integers as base64url strings

5 unstable releases

0.3.1 Apr 5, 2023
0.3.0 Jan 22, 2023
0.2.0 Oct 13, 2022
0.1.1 Oct 4, 2022
0.1.0 Oct 4, 2022

#792 in Encoding

Download history 927/week @ 2024-02-26 1053/week @ 2024-03-04 1347/week @ 2024-03-11 827/week @ 2024-03-18 576/week @ 2024-03-25 109/week @ 2024-04-01 78/week @ 2024-04-08 61/week @ 2024-04-15 22/week @ 2024-04-22 25/week @ 2024-04-29 8/week @ 2024-05-06 28/week @ 2024-05-13 34/week @ 2024-05-20 31/week @ 2024-05-27 46/week @ 2024-06-03 52/week @ 2024-06-10

165 downloads per month


910 lines


crates.io docs.rs Rust Validation license

A pure rust library for representing 64, 32 and 16 bit integers as base64url encoded strings.

base64url    i64                   u64
-----------  --------------------  --------------------
B21CkMCtWZA    535157120202267024    535157120202267024
fHH_W21Typg   8967229101212682904   8967229101212682904
kjsG-f3NhxI  -7909720649771415790  10537023423938135826
jHamKFSl5oM  -8325284168998721917  10121459904710829699

An integer is efficiently stored and manipulated in memory. However the integer cannot be sent to/from a web client in a url safe manor, without some encoding scheme. This library allows you to encode the integer to/from an base64url character string.

For a video of the underlying concept in action, see here.


  • Integers are made url safe
  • Encoded integers use fewer bytes as compared to hex or decimal encoding
  • Tests for RFC 4648 compliance where implemented from the start and across the entire libary
  • base64id is #![no_std] by default with no heap allocation required
  • base64id uses #![forbid(unsafe_code)]


I've used this concept a number of times in personal and work projects as I find it very useful. The problem is I've had to reimplement the functionality everytime.

The motivation for this library was to design and implement the core concept once, while paying attention to metrics such as performance, correctness and compatability.


Add the following to your Cargo.toml file

base64id = { version = "0.3", features = ["std"] }

For #![no_std] environments the std cargo feature can be omitted.


All work is done using the Id64, Id32 and Id16 structs.

The examples below use the Id64 struct, though any Id__ struct can be used equivalently.


You can convert an i64 or u64 into an Id64 as follows

use base64id::Id64;

fn main() {
    let int: i64 = 1;
    let id = Id64::from(int);

    println!("{id}"); // AAAAAAAAAAE


You can use FromStr and From<i64> to convert a String into an Id64 and then into an i64 as follows

use base64id::{Error, Id64};
use std::str::FromStr;

fn main() -> Result<(), Error> {
    let id_str = Id64::from_str("PDFehCFVGqA")?;
    let id_int = i64::from(id_str);

    println!("{}", id_int); // 4337351837722417824


Third Party Crates

Support for Serde, Rand and SQLx can be enabled through the use of optional cargo feature flags.


You can use the rand feature flag for working with the rand crate.

use base64id::Id64;
use rand::random;

let id: Id64 = random();

println!("{id}"); // 11 random base64url characters


You can use the serde feature flag to drive Serialize and Deserialize on Id64.

use base64id::Id64;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct Record {
    id: Id64,

let record = Record {
    id: Id64::from(0u64),

println!("{}", serde_json::to_string(&record)?); // {"id":"AAAAAAAAAAA"}


You can use the sqlx feature flag for using an Id64 with SQLx SQL commands.

use base64id::Id64;
use sqlx::{
    sqlite::{Sqlite, SqliteConnection},
use std::str::FromStr;

let id = Id64::from_str("IkoY0lQYRrI")?;
let mut conn = SqliteConnection::connect("sqlite::memory:").await?;

sqlx::query("CREATE TABLE sqlx (id INT PRIMARY KEY)")
    .execute(&mut conn)

sqlx::query("INSERT INTO sqlx VALUES (?)")
    .execute(&mut conn)

let output = sqlx::query_as::<Sqlite, Id64>("SELECT id FROM sqlx LIMIT 1")
    .fetch_one(&mut conn)

println!("{output}"); // IkoY0lQYRrI


Licensed under either of

at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


~130K SLoC