escape hex and octal, refactor error handling