Optimized string encoding.