<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PMA Media Group&#187; unpack</title>
	<atom:link href="http://www.pmamediagroup.com/tag/unpack/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pmamediagroup.com</link>
	<description>Unique Marketing Techniques and Strategies with Guaranteed Results!</description>
	<lastBuildDate>Fri, 27 Aug 2010 18:14:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using Ruby Unpack To Save Strings To Integers</title>
		<link>http://www.pmamediagroup.com/2009/04/using-ruby-unpack-to-save-strings-to-integers/</link>
		<comments>http://www.pmamediagroup.com/2009/04/using-ruby-unpack-to-save-strings-to-integers/#comments</comments>
		<pubDate>Fri, 03 Apr 2009 19:12:51 +0000</pubDate>
		<dc:creator>Aaron Murphy</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[unpack]]></category>

		<guid isPermaLink="false">http://www2.pmamediagroup.com/?p=43</guid>
		<description><![CDATA[What does unpack do? It&#8217;s used to take a previously packed string of usually binary data and unpack into it&#8217;s original binary data. Pack is a method of Array which returns a string. But I wanted to use it to store data in a set of integers.

So I figured I could use unpack to do [...]]]></description>
			<content:encoded><![CDATA[<p>What does unpack do? It&#8217;s used to take a previously packed string of usually binary data and unpack into it&#8217;s original binary data. Pack is a method of Array which returns a string. But I wanted to use it to store data in a set of integers.</p>
<p><span id="more-43"></span></p>
<p>So I figured I could use unpack to do the job. Here&#8217;s how:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#996600;">&quot;Hello World!&quot;</span>.<span style="color:#9900CC;">unpack</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'I*C*'</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>This gave me an array of integers: [1819043144, 1461726319, 1684828783, 33]</p>
<p>That last one is the carry over from the size of integer to character. I used the &#8220;I*&#8221; format for unsigned integer and &#8220;C*&#8221; for unsigned character. The asterisks tells unpack to repeat as many times as it can for that data type.</p>
<p>So you could also do:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#996600;">&quot;Hello, World!&quot;</span>.<span style="color:#9900CC;">unpack</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'C*'</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>to get [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33], which is also an easy way to get an array of bytes for a string.</p>
<p>References: <a href="http://www.ruby-doc.org/core/classes/String.html">Ruby Doc Class: String</a><br />
<a href="http://www.ruby-doc.org/core/classes/Array.html">Ruby Doc Class: Array</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.pmamediagroup.com/2009/04/using-ruby-unpack-to-save-strings-to-integers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: www.pmamediagroup.com @ 2012-02-05 03:06:10 -->
