Refactoring to reduce redundant code for byte encoding.