Fix some bugs and implement full encode/decode.
Add some tests for hex encoding.