<?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>Unlikely Teacher &#187; Binary</title>
	<atom:link href="http://unlikelyteacher.com/tag/binary/feed/" rel="self" type="application/rss+xml" />
	<link>http://unlikelyteacher.com</link>
	<description>1. Share Everything* [Programming Gotchas, Technology News, Insights on Living and Everything in Between]</description>
	<lastBuildDate>Fri, 03 Sep 2010 04:12:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>PHP writeUTF implementation</title>
		<link>http://unlikelyteacher.com/2009/05/12/php-writeutf-implementation/</link>
		<comments>http://unlikelyteacher.com/2009/05/12/php-writeutf-implementation/#comments</comments>
		<pubDate>Tue, 12 May 2009 04:02:50 +0000</pubDate>
		<dc:creator>paul</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Binary]]></category>
		<category><![CDATA[pack()]]></category>

		<guid isPermaLink="false">http://dayg.slingandstoneweb.com/?p=371</guid>
		<description><![CDATA[We currently have an application in Groovy/Grails/Java that we&#8217;re slowly porting to PHP (for more affordable hosting costs). We encountered a problem when we needed to convert one of our routines for writing out a binary file. We were using Java&#8217;s DataOutputStream.writeUTF(String) method and had a hard time trying to write binary data in PHP. [...]]]></description>
			<content:encoded><![CDATA[<p>We currently have an application in Groovy/Grails/Java that we&#8217;re slowly porting to PHP (for more affordable hosting costs).</p>
<p>We encountered a problem when we needed to convert one of our routines for writing out a binary file. We were using Java&#8217;s DataOutputStream.writeUTF(String) method and had a hard time trying to write binary data in PHP.</p>
<p>After much research on the web, we found a method in PHP called <a href="http://www.php.net/pack">pack()</a>. Below is our PHP implementation of Java&#8217;s DOS.writeUTF(String).</p>
<pre class="brush: php">
	public static function writeUTF($string) {
		$utfString = utf8_encode($string);
		$length = strlen($utfString);
		print(pack(&quot;n&quot;, $length));
		print($utfString);
		flush();
	}
</pre>
<p>We don&#8217;t claim this to be the exact equivalent of the method, but it gets the job done and the receiving end of the file was able to parse the binary file properly with the new PHP implementation with no modifications to the client code.</p>
<p>Please checkout the <a href="http://www.php.net/pack">pack(</a>) documentation for more details.</p>
]]></content:encoded>
			<wfw:commentRss>http://unlikelyteacher.com/2009/05/12/php-writeutf-implementation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
