将二进制数据直接存储在远程存储中而不进行任何编组处理是灾难的根源。我不建议这样做:可以使用许多序列化协议来使二进制数据独立于平台。
也就是说,回答您的问题:
// This is the keyint k[3] = {11,22,33};// This is the valueint v[4] = {0,1,2,3};redisReply *reply = 0;// Store the key/value: note the usage of sizeof to get the size of the arrays (in bytes)reply = redisCommand(context, "SET %b %b", k, (size_t) sizeof(k), v, (size_t) sizeof(v) );if (!reply) return REDIS_ERR;freeReplyObject(reply);// Now, get the value back, corresponding to the same keyreply = redisCommand(context, "GET %b", k, (size_t) sizeof(k) );if ( !reply ) return REDIS_ERR;if ( reply->type != REDIS_REPLY_STRING ) { printf("ERROR: %s", reply->str);} else { // Here, it is safer to make a copy to be sure memory is properly aligned int *val = (int *) malloc( reply->len ); memcpy( val, reply->str, reply->len); for (int i=0; i<reply->len/sizeof(int); ++i ) printf("%dn",val[i]); free( val );}freeReplyObject(reply);请注意,只有在确保所有Redis客户端在具有相同字节序和相同sizeof(int)的系统上运行时,此类代码才有效。



