Representing Ids as Strings

Ids are represented as strings with a Base64 like URL-safe encoding. The encoding is based on the following index table.

ValueChar  ValueChar  ValueChar  ValueChar
0- 16F 32V 48k
1017G33W49l
2118H34X50m
3219I35Y51n
4320J36Z52o
5421K37_53p
6522L38a54q
7623M39b55r
8724N40c56s
9825O41d57t
10926P42e58u
11A27Q43f59v
12B28R44g60w
13C29S45h61x
14D30T46i62y
15E31U47j63z

When ids are converted to Strings they are treated as unsigned values. Apart from toString, you can use toShortString which creates shorter strings omitting zeros from the beginning. Id's companion object contains a string extractor which can be used for pattern matching.

import gr.jkl.uid.Id

val id = Id(-9217076510208286673L)

val stringId = id.toString
// stringId: String = --LMQy4R1-j

val shortStringId = id.toShortString
// shortStringId: String = LMQy4R1-j

stringId match {
  case Id(a) => println(s"Valid id: $a")
  case _      => println("Invalid id")
}
// Valid id: --LMQy4R1-j

shortStringId match {
  case Id(a) => println(s"Valid id: $a")
  case _      => println("Invalid id")
}
// Valid id: --LMQy4R1-j

Warning: Short string decoding is broken on versions 1.0 and 1.1.

Fork me on GitHub