A system I'm building needs to convert non-negative Ruby integers into shortest-possible UTF-8 string (should be octet string; see Edit below) values. The only requirement on the strings is that their lexicographic order be identical to the natural order on integers.
What's the best Ruby way to do this?
We can assume the integers are 32 bits and the sign bit is 0. This is successful:
(i >> 24).chr + ((i >> 16) & 0xff).chr + ((i >> 8) & 0xff).chr + (i & 0xff).chr
But it appears to be 1) garbage-intense and 2) ugly. I've also looked at pack
solutions, but these don't seem portable due to byte order.
FWIW, the application is Redis hash field names. Building keys may be a performance bottleneck, but probably not. This question is mostly about the "Ruby way".
Edit
Abpve I should have said "shortest possible string of octets" rather than UFT-8, since this is what Redis actually
stores for field keys. @Mark Reed's excellent suggestion to try true UTF-8 packing ssems to work. The redis
gem I am using seems to properly convert extended codes to octet sequences for Redis: For example,
REDIS.hset('hash', [0x12345678].pack('U'), 'foo')
works fine. But then
REDIS.hkeys('hash')
returns
"\xFC\x92\x8D\x85\x99\xB8"
I need to verify the lexicographic order of these strings is correct, but it looks good so far.
End edit