What is a binary safe string ? Is it just a string with length attached in the string itself ? Why are they called binary safe then ? Not sure that I should tag redis, but read this term while reading redis doc.
4 Answers
A binary safe string is one that can consist of any characters (bytes).
For example, many programming languages use the 0x00 character as an end-of-string marker, so in that sense a binary safe string is one that can consist of these.
Redis string is a sequence of bytes. Strings in Redis are binary safe, meaning they have a known length not determined by any special terminating characters. Thus, you can store anything up to 512 megabytes in one string.
Binary safe string does not make any assumption about its length or terminating character. For example, in Redis (since you have mentioned Redis in your question), syntax for a binary safe bulk string is "$str_len\r\nbinary_safe_string\r\n". Here, the length of the string is made part of the string representation. With this, you can save anything as a string, say a JPEG image. Refer to Redis documentation for more details.
The definition of "binary safe string" in Redis's documentation is this.
Redis Strings are binary safe, this means that a Redis string can contain any kind of data, for instance a JPEG image or a serialized Ruby object.
Therefore, binary security is relative to the c language string ended with the character '\0'.