Fix when the value length is not a multiple of 3

- adds tests to reveal the problem
- checks the length of the value to produce the correct format