--- /dev/null
+USING: base62 math.parser strings tools.test ;
+
+{ "" } [ "" >base62 >string ] unit-test
+{ "" } [ "" base62> >string ] unit-test
+
+{ "0" } [ B{ 0 } >base62 >string ] unit-test
+{ B{ 0 } } [ "0" base62> ] unit-test
+
+{ "00" } [ B{ 0 0 } >base62 >string ] unit-test
+{ B{ 0 0 } } [ "00" base62> ] unit-test
+
+{ "Q0DRQksv" } [ "SIMPLE" >base62 >string ] unit-test
+{ "SIMPLE" } [ "Q0DRQksv" base62> >string ] unit-test
--- /dev/null
+! Copyright (C) 2020 John Benediktsson.
+! See http://factorcode.org/license.txt for BSD license.
+
+USING: base64.private byte-arrays checksums checksums.sha
+io.binary kernel kernel.private literals math sequences ;
+
+IN: base62
+
+ERROR: malformed-base62 ;
+
+<PRIVATE
+
+<<
+CONSTANT: alphabet $[
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
+ >byte-array
+]
+>>
+
+PRIVATE>
+
+: ch>base62 ( ch -- ch )
+ alphabet nth ; inline
+
+: base62>ch ( ch -- ch )
+ $[ alphabet alphabet-inverse ] nth
+ [ malformed-base62 ] unless* { fixnum } declare ; inline
+
+:: >base62 ( seq -- base62 )
+ BV{ } clone :> accum
+ seq [ zero? not ] find [ drop seq length ] unless :> i
+ seq i tail-slice be>
+ [ 62 /mod ch>base62 accum push ] until-zero
+ i alphabet first '[ _ accum push ] times
+ accum reverse! B{ } like ;
+
+:: base62> ( base62 -- seq )
+ BV{ } clone :> accum
+ base62 alphabet first '[ _ = not ] find
+ [ drop base62 length ] unless :> i
+ 0 base62 [ [ 62 * ] dip base62>ch + ] i each-from
+ [ 256 /mod accum push ] until-zero
+ i [ 0 accum push ] times
+ accum reverse! B{ } like ;