Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8890609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:32:28+00:00 2026-06-14T22:32:28+00:00

A system I’m building needs to convert non-negative Ruby integers into shortest-possible UTF-8 string

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:32:30+00:00Added an answer on June 14, 2026 at 10:32 pm

    If it has to be valid UTF-8, you’re not going to get much of an improvement over just encoding the code-point as a UTF-8 character; one of the features of UTF-8 is that encoded characters sort in the proper numeric order, and it only uses the minimal number of bytes necessary under the rules of the format.

    [i].pack('U')
    

    Note that UTF-8 is byte-oriented, so there are no endianness concerns.

    If you didn’t actually mean UTF-8, then please clarify what you did mean.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

System.BitArray only implements the non-generic IEnumerable, which returns an Object for the IEnumerator.Current property.
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
System: Windows Server 2008 Ruby 192 in 'delete': Permission denied Errno:EACCES the line: File.delete('filename.ext')
System.IO.StreamReader sr = new System.IO.StreamReader(Server.MapPath(strheadlinesid1)); string line; while (sr.Peek() != -1) { line =
System.IO.Directory.GetFiles() returns a string[] . What is the default sort order for the returned
System.out.print(My string: ); My string: BUILD SUCCESSFUL (total time: 1 second) System.out.print(My string );
System.Collections.Specialized.NameObjectCollectionBase has two similar properties: string[] AllKeys NameObjectCollectionBase.KeyCollection Keys Do they provide different sets
System.out.println(300 + ((160 - 300) / (240 - 20)) * (27 - 20)); //Should
System: OS: Linux (Debian) rails: 3.1.4 ruby: 1.9.3p194 rvm: 1.14.5 cucumber: 1.2.1 cucumber-rails: 1.3.0
System.out.println(Please enter the required word :); Scanner scan = new Scanner(System.in); String word =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.