Fixed infinite loop in encodeNonASCII method

Plus I added some tests for the same method.