Discussions link: https://github.com/redis/redis/discussions/10883
There is only a method RedisModule_StringToLongLong to get integer from RedisModuleString. But the range of unsigned long and long long are different (18446744073709551615 and 9223372036854775807 on my machine), so we can't get unsigned long by RedisModule_StringToLongLong.
Is there any approach to get unsigned long directly from RedisModuleString or get the raw string?
The only way I come up with is a little hacky. IT IS: construct a struct totally same as robj (since we can't use robj when writing Redis Module, or we can?), and cast RedisModuleString to robj forcibly. Then we can get the raw string by robj.ptr.
Please tell me what is the best way to get unsigned long from RedisModuleString.
Comment From: zuiderkwast
Hi! Since the range of long long is greater than long, you can simply cast the result to long afterwards, after checking that it's within the range 0..ULONG_MAX, can't you?
If you want to get the raw string from a RedisModuleString, you can use RedisModule_StringPtrLen. It also gives you the length of the string at the same time.
Comment From: oranagra
i don't think casting will work, it'll get out of range and return an error.
i think your options are either to wait (or make a PR) to add RedisModule_StringToULongLong, or use RedisModule_StringPtrLen and do the conversion yourself using strtoull.
Comment From: RinChanNOWWW
Thank you guys for answering my question, I didn't note that RedisModule_StringPtrLen will return the raw string, my mistake.
And I will try to make a PR to add RedisModule_StringToULongLong.
Since the range of
long longis greater thanlong.
BTW, the ranges of these two types are the same on my machine.