util: convert string to bytes before writing to file
minor indent style cleanup
make tests compatible with py3