Get操作对Redis位图返回奇怪的输出

编程入门 行业动态 更新时间:2024-10-27 13:28:36
本文介绍了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位图返回奇怪的输出

本文发布于:2023-10-11 17:49:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1482399.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位图   奇怪   Redis

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!