I have some data in bytes, and I want to put them into Redis, but Redis only accepts binary safe string, and my data has some binary non-safe bytes. So how can I convert these bytes into binary safe string so that I can save them to Redis?
Base64 works for me, but it makes data larger, any better idea?
UPDATE: I want to serialize my protobuf object to Redis, and the serialized data has '\x00', so when I read the data from Redis, I can not deserialize the data to object. Then I tried base64, it works fine, but with larger size.
So I want to figure out how to serialize binary data (protobuf object) to Redis safely and with smaller size
解决方案
You could try ISO-8859-1 encoding. This uses a one to one mapping between bytes and chars.
This could still result in corruption depending on why Redis need this "binary safe" string. You may have to use base64.