Q: I use redisAppendCommandArgv(redisContext c, int argc, const char *argv, const size_t *argvlen) and add the following data(after zipped with libz, there is "\x00" in the data) into redis successfully.
But when I read the data from redis, the reply->str is "@D@x\x9c\x01i", not incomplete.
How can I get the complete data? Any suggestion or help? Thanks a lot.
Data:
"x\x9c\x01i\x00\x96\xff\xe5\xae\x9d\xe8\x8a\xae\xe9\x9b\x85\xe8\x8e\x892016\xe5\xa4\x8f\xe5\xad\xa3\xe6\x96\xb0\xe6\xac\xbe\xe5\xa6\x88\xe5\xa6\x88\xe8\xa3\x85\xe9\x9b\xaa\xe7\xba\xba\xe8\xa1\xab\xe8\xbf\x9e\xe8\xa1\xa3\xe8\xa3\x99\xe4\xb8\xad\xe8\x80\x81\xe5\xb9\xb4\xe4\xb8\xad\xe9\x95\xbf\xe6\xac\xbe\xe6\xb8\x85\xe5\x87\x89\xe7\x9f\xad\xe8\xa2\x96\xe5\xa4\x8fFYY-1 XXXL \xe7\xba\xa2\xe8\x89\xb2\xae;D\xa0"
Check reply->len which should give you the correct length of the returned data.
Strings in C are nul-terminated and thus all functions trying to _display_ the returned value will stop on encountering the first nul byte. The data is still there.
Thanks for your quickly reply. My writing and reading are two different process锛宻o I should write the length of the data into redis and read according to the length, is it right?
No.
Redis and hiredis are both perfectly binary-safe.
The Redis protocol communicates the length of the returned data upfront, so hiredis can read the exact amount.
If you use redisAppendCommandArgv you specify the exact amount using argvlen for each argument. If you set this to the byte size of your data, hiredis will write exactly that amount (careful: do not use strlen on your data, it will only return the length up to the first nul byte).
Reading it back works just the same. hiredis will make sure to read the exact amount that is in the database, which for your data should be 116. You can check that on reply->len
Thanks a lot again, It works now!
Wrong code: string tmp=reply->str;
Right code: string tmp(reply->str, reply->len);
Most helpful comment
Thanks a lot again, It works now!
Wrong code:
string tmp=reply->str;Right code:
string tmp(reply->str, reply->len);