4

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

5
  • 3
    Do you have details of exactly what is meant by "binary safe string"? It's not a standard term. Commented Dec 13, 2012 at 10:30
  • Reading this, it seems that for Redis, binary safe just means that Redis won't change the content. Unless I'm missing something (prettly likely, as I haven't used Redis before), you don't need to do anything to your data. Commented Dec 13, 2012 at 10:35
  • @Pablo I've update my question for more details Commented Dec 13, 2012 at 10:43
  • @JonSkeet I've update my question for more details Commented Dec 13, 2012 at 10:43
  • 2
    @MartinLuo: Your edit doesn't give any more information about what is meant by "binary safe string". Commented Dec 13, 2012 at 10:48

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I want to find a way with smaller size, base64 works for me now, but I don't think it is good enough
If using ISO-8859-1 works you may find it doesn't increase the size. Base64 turns 3 bytes into 4 chars, this turns 1 byte into 1 char (and visa-versa)
-1

The only safe way to serialize a binary object (such as a protobuf object) is to base64 encode it. Base64 has a 33% overhead but gives you the ability to safely convert from arbitrary binary data to text (such as for use in an xml file) and back.

1 Comment

33% overhead or 166% overhead? In many environments, every three bytes of data turns into will turn into four two-byte characters, i.e. 8 bytes total?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.