Search code examples
redisazure-redis-cache

How to read Redis binary values in redis console


I store a byte array with 5 bytes in a Redis entry. Writing and reading using a client library works and expected, but when I try to read the value in a Redis console I get something I don't know how to interpret:

>get keyHere
"\x02\x8e\x8b\x0cb"

There is something I clearly don't understand because \x0cb is not a hex value for a byte and there are only 4 \x (and I expected 5 for 5 bytes).

Confused, I decided to performed an experiment. I educated myself about how to set raw bytes; I set an entry's value to "\x01\x07" and read it back. I expected "\x01\x07" but the read value is shown as "\x01\a".

>set "3" "\x01\x07"
OK
>get 3
"\x01\a" 

How should I read entries in a Redis cache in the Redis console to see raw bytes?


Solution

  • If the byte is not printable, redis-cli prints the hex format, otherwise, it prints the c-escpaped sequence.

    because \x0cb is not a hex value for a byte and there are only 4 \x (and I expected 5 for 5 bytes)

    The first 4 bytes are not printable, so they are printed as hex format. The last byte is the b, which is printable.

    I expected "\x01\x07" but the read value is shown as "\x01\a".

    \x07's c-escaped sequence is \a, and is printable.

    How should I read entries in a Redis cache in the Redis console to see raw bytes?

    If you need the raw bytes (which might not be printable), you can specify the --raw option when running redis-cli.