Ids are represented as strings with a Base64 like URL-safe encoding. The encoding is based on the following index table.
Value | Char | Value | Char | Value | Char | Value | Char | |||
---|---|---|---|---|---|---|---|---|---|---|
0 | - | 16 | F | 32 | V | 48 | k | |||
1 | 0 | 17 | G | 33 | W | 49 | l | |||
2 | 1 | 18 | H | 34 | X | 50 | m | |||
3 | 2 | 19 | I | 35 | Y | 51 | n | |||
4 | 3 | 20 | J | 36 | Z | 52 | o | |||
5 | 4 | 21 | K | 37 | _ | 53 | p | |||
6 | 5 | 22 | L | 38 | a | 54 | q | |||
7 | 6 | 23 | M | 39 | b | 55 | r | |||
8 | 7 | 24 | N | 40 | c | 56 | s | |||
9 | 8 | 25 | O | 41 | d | 57 | t | |||
10 | 9 | 26 | P | 42 | e | 58 | u | |||
11 | A | 27 | Q | 43 | f | 59 | v | |||
12 | B | 28 | R | 44 | g | 60 | w | |||
13 | C | 29 | S | 45 | h | 61 | x | |||
14 | D | 30 | T | 46 | i | 62 | y | |||
15 | E | 31 | U | 47 | j | 63 | z |
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.