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

  • Home
  • SEARCH
  • 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 7865019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:57:36+00:00 2026-06-02T23:57:36+00:00

In ruby 1.9.3, I can get the codepoints of a string: > foo\u00f6.codepoints.to_a =>

  • 0

In ruby 1.9.3, I can get the codepoints of a string:

> "foo\u00f6".codepoints.to_a
 => [102, 111, 111, 246] 

Is there a built-in method to go the other direction, ie from integer array to string?

I’m aware of:

# not acceptable; only works with UTF-8
[102, 111, 111, 246].pack("U*")

# works, but not very elegant
[102, 111, 111, 246].inject('') {|s, cp| s << cp }

# concise, but I need to unshift that pesky empty string to "prime" the inject call
['', 102, 111, 111, 246].inject(:<<)

UPDATE (response to Niklas’ answer)

Interesting discussion.
pack("U*") always returns a UTF-8 string, while the inject version returns a string in the file’s source encoding.

#!/usr/bin/env ruby
# encoding: iso-8859-1

p [102, 111, 111, 246].inject('', :<<).encoding
p [102, 111, 111, 246].pack("U*").encoding
# this raises an Encoding::CompatibilityError
[102, 111, 111, 246].pack("U*") =~ /\xf6/

For me, the inject call returns an ISO-8859-1 string, while pack returns a UTF-8. To prevent the error, I could use pack("U*").encode(__ENCODING__) but that makes me do extra work.

UPDATE 2

Apparently the String#<< doesn’t always append correctly depending on the string’s encoding. So it looks like pack is still the best option.

[225].inject(''.encode('utf-16be'), :<<)  # fails miserably
[225].pack("U*").encode('utf-16be')  # works
  • 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-02T23:57:37+00:00Added an answer on June 2, 2026 at 11:57 pm

    The most obvious adaption of your own attempt would be

    [102, 111, 111, 246].inject('', :<<)
    

    This is however not a good solution, as it only works if the initial empty string literal has an encoding that is capable of holding the entire Unicode character range. The following fails:

    #!/usr/bin/env ruby
    # encoding: iso-8859-1
    p "\u{1234}".codepoints.to_a.inject('', :<<)
    

    So I’d actually recommend

    codepoints.pack("U*")
    

    I don’t know what you mean by “only works with UTF-8”. It creates a Ruby string with UTF-8 encoding, but UTF-8 can hold the whole Unicode character range, so what’s the problem? Observe:

    irb(main):010:0> s = [0x33333, 0x1ffff].pack("U*")
    => "\u{33333}\u{1FFFF}"
    irb(main):011:0> s.encoding
    => #<Encoding:UTF-8>
    irb(main):012:0> [0x33333, 0x1ffff].pack("U*") == [0x33333, 0x1ffff].inject('', :<<)
    => true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Ruby language, how can I get the number of lines in a string?
I have a string like this in Ruby word=0 id=891efc9a-2210-4beb-a19a-5e86b2f8a49f How can I get
Ruby can add methods to the Number class and other core types to get
So I know that you can get all instance variables in Ruby by calling
I was doing a little test to try Ruby's pty and I can't get
I am using ruby 1.9.2 and sinatra 1.3.2, Datamapper I get error: can't convert
I can't seem to get the Ruby Shoes packager to correctly create an exe
When calling each on a hash in ruby, you can get the key and
When you use a system call in a Ruby script, you can get the
Is there any way I can get the controller that's about to handle the

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.