--- /dev/null
+
+USING: leb128 tools.test ;
+
+{ B{ 0xe5 0x8e 0x26 } } [ 624485 >leb128 ] unit-test
+{ 624485 } [ B{ 0xe5 0x8e 0x26 } leb128> ] unit-test
+
+{ B{ 0xc0 0xbb 0x78 } } [ -123456 >leb128 ] unit-test
+{ -123456 } [ B{ 0xc0 0xbb 0x78 } leb128> ] unit-test
--- /dev/null
+! Copyright (C) 2023 John Benediktsson
+! See https://factorcode.org/license.txt for BSD license
+
+USING: combinators.short-circuit kernel make math sequences ;
+
+IN: leb128
+
+:: >leb128 ( n -- byte-array )
+ [
+ n [
+ [ -7 shift dup ] [ 0x7f bitand ] bi :> ( i b )
+ {
+ [ i zero? b 6 bit? not and ]
+ [ i -1 = b 6 bit? and ]
+ } 0|| [ f b ] [ t b 0x80 bitor ] if ,
+ ] loop drop
+ ] B{ } make ;
+
+: leb128> ( byte-array -- n )
+ [ 0 [ [ 0x7f bitand ] [ 7 * shift ] bi* + ] reduce-index ] keep
+ dup last 6 bit? [ length 7 * 2^ neg bitor ] [ drop ] if ;