First of: Redis doesn't care. All it sees are some bytes, it doesn't
know anything about encoding.
What you are seeing is how your client library deals with it. At least
the pure-Ruby connection will force an encoding back on the string ([1] [2]).
(I'd like to get all the encoding stuff out of redis-rb though, so it will always just return bytes)
For whatever reason it might be that something fails in between, your
external encoding is different or a dozen other reasons.
If you know that you will get UTF-8, just force it to be interpreted as UTF-8.
You can always write a wrapper method to deal with it.
[1]:
https://github.com/redis/redis-rb/blob/master/lib/redis/connection/ruby.rb#L396
[2]:
https://github.com/redis/redis-rb/blob/master/lib/redis/connection/command_helper.rb#L35
On Fri, Aug 12, 2016 at 04:41:14AM -0700, LO Yi wrote:
> I am using Sinatra + Redis to make a tiny personal blog. Basically I have
> two pages
>
>
> 1. * the page '/' use for show people what are my posts.*
> 2. *the page '/write' is the background that I post my blog.*
>
> *and it look like this*
>
> <
https://lh3.googleusercontent.com/-lS9BpBOla5U/V62yMlhskyI/AAAAAAAAABs/LSWqX5VDP6EzPSWtH_-nJNKdDdH4upDJgCLcB/s1600/0DFF426E-B9ED-4A49-BE19-215830F6D908.png>
>
> line 21 is where my Chinese post is, I know that Redis cannot save the
> original Chinese into database but encodes it.
>
> like for example when I type in "哈哈", Redis converts this to
> "\xe5\x93\x88\xe5\x93\x88".
>
> So when I get this value from Redis by the code in line 21, Ruby seems not
> realize this is Chinese, and I will meet the error of "
> incompatible character encodings: ASCII-8BIT and UTF-8
> <
http://stackoverflow.com/questions/5286117/incompatible-character-encodings-ascii-8bit-and-utf-8>
> "
>
> I have tried to put "#encoding:UTF-8" at the first line, it doesn't work.
> I have tried put "set :default_encoding,"utf-8" in my configure as well,
> and that doesn't work too.
> I found that convert my value by using .force_encoding('utf-8') could solve
> this problem, but I think there should be a better way to solve this
> problem. Otherwise, I have to use this method in every value that I get
> from Redis.
>
> Thanks in advance!
>
>
>
> --
> You received this message because you are subscribed to the Google Groups "Redis DB" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
redis-db+u...@googlegroups.com.
> To post to this group, send email to
redi...@googlegroups.com.
> Visit this group at
https://groups.google.com/group/redis-db.
> For more options, visit
https://groups.google.com/d/optout.