Encode string for os.write()