Bugfix. Lengt using bytes with the value 0 didn't work well

Corner case when there was a byte 0x00 in the length.. For example 256 generated 0x82 and no more, but the right answer is 0x82, 0x01, 0x00
I added a test for this case and using the tests refactor the code. Now it is more easy to understand

I apologise for no detect this corner case before.