Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Assuming your data is in an array like so (each element is a byte, and further, from the description you posted, no larger than 127 in value, that is, a 7-bit ASCII character):</p> <pre><code>array =[104, 101, 108, 108, 111] string = array.pack("c*") </code></pre> <p>After this, string will contain "hello", which is what I believe you're requesting.</p> <p>The pack method "Packs the contents of arr into a binary sequence according to the directives in the given template string".</p> <p>"c*" asks the method to interpret each element of the array as a "char". Use "C*" if you want to interpret them as <em>unsigned</em> chars.</p> <p><a href="http://ruby-doc.org/core/classes/Array.html#M002222" rel="nofollow noreferrer">http://ruby-doc.org/core/classes/Array.html#M002222</a></p> <p>The example given in the documentation page uses the function to convert a string with Unicode characters. In Ruby I believe this is best done using Iconv:</p> <pre><code>require "iconv" require "pp" #Ruby representation of unicode characters is different unicodeString = "This unicode string contains two characters " + "with codes outside the ASCII code range, " + "Pi (\342\x03\xa0) and Sigma (\342\x03\xa3)."; #printing original string puts unicodeString i = Iconv.new("ASCII//IGNORE","UTF-8") #Printing converted string, unicode characters stripped puts i.iconv(unicodeString) bytes = i.iconv(unicodeString).unpack("c*") #printing array of bytes of converted string pp bytes </code></pre> <p>Read up on Ruby's Iconv <a href="http://ruby-doc.org/core/classes/Iconv.html#M001164" rel="nofollow noreferrer">here</a>.</p> <p>You might also want to check <a href="https://stackoverflow.com/questions/144380/ruby-how-to-break-a-potentially-unicode-string-into-bytes">this question</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload