Added CompactEncode function with unit tests