Get操作对Redis位图返回奇怪的输出 [英] Get operation on Redis bitmap returns weird output

查看:210
本文介绍了Get操作对Redis位图返回奇怪的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在做一个redis get操作,以获取键。

 咖啡> redis.setbita,7,1 
true
coffee> redis.setbitd,4,1
true
coffee> redis.setbitg,1,1
true
coffee> redis.setbith,0,1

输出

 咖啡> redis.geta,(err,res) - > console.log res.toString()。charCodeAt(0)
true
coffee> 1

咖啡> redis.getd,(err,res) - > console.log res.toString()。charCodeAt(0)
true
coffee> 8

coffee> redis.getg,(err,res) - > console.log res.toString()。charCodeAt(0)
true
coffee> 64

咖啡> redis.geth,(err,res) - > console.log res.toString()。charCodeAt(0)
true
coffee> 65533

我的问题是用h键设置第0位。返回128但返回65533.为什么是这样的?



我的最终目标是从二进制的redis获取位图,以便我可以获得哪些用户在那一天活动。

解决方案

由于utf-8编码,发生此错误。当我们将第0位设置为1时,它不遵循utf-8规则。现在,当我们尝试获取它,我们得到替换字符


U + FFFD 替换字符用于替换未知或
无法表示的字符


,当我们对它进行charCodeAt时,我们将获得65533.



请在这里阅读 UTF-8 Specials Unicode Block


I am doing setbit operation in redis to mark which users have been online on a particular day.

I am doing a redis get operation to get the value of the key.

coffee> redis.setbit "a",7,1
true
coffee> redis.setbit "d",4,1
true
coffee> redis.setbit "g",1,1
true
coffee> redis.setbit "h",0,1

And the output is

coffee> redis.get "a",(err,res)->console.log res.toString().charCodeAt(0)
true
coffee> 1

coffee> redis.get "d",(err,res)->console.log res.toString().charCodeAt(0)
true
coffee> 8

coffee> redis.get "g",(err,res)->console.log res.toString().charCodeAt(0)
true
coffee> 64

coffee> redis.get "h",(err,res)->console.log res.toString().charCodeAt(0)
true
coffee> 65533

My problem is with the "h" key where I am setting the 0th bit 1. It should return 128 but returns 65533.Why is this so?

My end goal is to get bitmap from redis in binary so that I can get which users were active on that particular day.

解决方案

This error occurs because of utf-8 encoding. When we set the 0th bit as 1, it does not follow utf-8 rules. Now when we try to get it we get the replacement Character

U+FFFD � replacement character used to replace an unknown or unrepresentable character

and when we do a charCodeAt on it we will get 65533.

Read here UTF-8 and Specials Unicode Block

这篇关于Get操作对Redis位图返回奇怪的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆