<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://95.179.246.60/mediawiki/index.php?action=history&amp;feed=atom&amp;title=Read%2FWrite_a_binary_file</id>
	<title>Read/Write a binary file - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://95.179.246.60/mediawiki/index.php?action=history&amp;feed=atom&amp;title=Read%2FWrite_a_binary_file"/>
	<link rel="alternate" type="text/html" href="http://95.179.246.60/mediawiki/index.php?title=Read/Write_a_binary_file&amp;action=history"/>
	<updated>2026-05-08T12:41:50Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.17</generator>
	<entry>
		<id>http://95.179.246.60/mediawiki/index.php?title=Read/Write_a_binary_file&amp;diff=105&amp;oldid=prev</id>
		<title>Nmingott: imported material</title>
		<link rel="alternate" type="text/html" href="http://95.179.246.60/mediawiki/index.php?title=Read/Write_a_binary_file&amp;diff=105&amp;oldid=prev"/>
		<updated>2025-05-04T20:37:22Z</updated>

		<summary type="html">&lt;p&gt;imported material&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;#039;&amp;#039;&amp;#039;CAVEAT&amp;#039;&amp;#039;&amp;#039;. This recipe was cooked in Linux and uses the file &amp;lt;code&amp;gt;/dev/urandom&amp;lt;/code&amp;gt; which will be present in all Unix-like systems as far as the author knows. So, Linux, Mac, BSD are all fine.&lt;br /&gt;
&lt;br /&gt;
Sometimes your file contains stuff which is not intended to be interpreted as a sequence of characters. This can be the case, for example, if you are reading/writing some compressed data, some audio file, a big matrix, a picture etc.&lt;br /&gt;
&lt;br /&gt;
== Reading from a binary file ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Problem&amp;#039;&amp;#039;&amp;#039;. I want to have a quite good random number and I know than most Unix systems provide a file &amp;lt;code&amp;gt;/dev/urandom&amp;lt;/code&amp;gt; where I can read as many as I want. Please see your operating system documentation of the file to know how it works. This is a classic example of a binary stream, when I read from &amp;lt;code&amp;gt;/dev/urandom&amp;lt;/code&amp;gt; I am supposed to read byte per byte, e.g. it would make not much sense to try to read a line. Also, this example is nice because you are not supposed to read in all the file, since this file has no end! You must read only a certain amount of it.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Solution&amp;#039;&amp;#039;&amp;#039;. Follow the sequence of commands below to see how you can read from &amp;lt;code&amp;gt;/dev/urandom&amp;lt;/code&amp;gt; as a binary stream.&lt;br /&gt;
 &amp;quot;Make a ReadStream from a file&amp;quot;&lt;br /&gt;
 rs _ &amp;#039;/dev/urandom&amp;#039; asFileEntry readStream. &lt;br /&gt;
 rs class.       &amp;quot;=&amp;gt; StandardFileStream &amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 &amp;quot; generally you may want to read a binary stream as a sequence of bytes.&amp;quot;&lt;br /&gt;
 rs useBytes . &lt;br /&gt;
 &lt;br /&gt;
 &amp;quot; get the fisrt byte&amp;quot;&lt;br /&gt;
 rs next.             &amp;quot;=&amp;gt; 7 &amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 &amp;quot;get the following 10 bytes&amp;quot;&lt;br /&gt;
 tmp _ rs next: 10.    &amp;quot;=&amp;gt;  #[192 101 93 30 46 183 55 186 231 66] &amp;quot;&lt;br /&gt;
 tmp class.            &amp;quot;=&amp;gt; ByteArray &amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 &amp;quot; get &amp;#039;tmp&amp;#039; in hex format, spaces added for readability by hand &amp;quot;&lt;br /&gt;
 tmp hex. &amp;quot; &amp;#039;EF F6 F0 1A 78 14 C0 02 73 1C&amp;#039; &amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 &amp;quot; get &amp;#039;tmp&amp;#039; as base64, will make it shorter &amp;quot;&lt;br /&gt;
 tmp base64Encoded .    &amp;quot;=&amp;gt; &amp;#039;15vGaEu2IU1NAQ==&amp;#039; &amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 &amp;quot; let&amp;#039;s see what comes back if we read in binary mode&amp;quot;&lt;br /&gt;
 tmp2 _ rs next.        &amp;quot;=&amp;gt; 238 &amp;quot;&lt;br /&gt;
 tmp2 class.            &amp;quot;=&amp;gt; SmallInteger &amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 &amp;quot; we may want to read the binary stream as characters &amp;quot;&lt;br /&gt;
 rs useCharacters .&lt;br /&gt;
 rs next: 10.           &amp;quot;=&amp;gt; &amp;#039;)Orý»¯ÊÂ³&amp;#039; &amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 &amp;quot; when you finished remember to close the stream. &amp;quot;&lt;br /&gt;
 rs close.&lt;br /&gt;
&lt;br /&gt;
== TODO: Writing a binary file ==&lt;br /&gt;
To remember here is that &amp;lt;code&amp;gt;writeStream&amp;lt;/code&amp;gt; asks confirmation if the file already exists, to have no question asked use &amp;lt;code&amp;gt;forceWriteStream&amp;lt;/code&amp;gt;.&lt;br /&gt;
----Dr. Nicola Mingotti last updated this on 3-Sep-2021. Examples where run in Cuis-5.0-4815.image.&lt;/div&gt;</summary>
		<author><name>Nmingott</name></author>
	</entry>
</feed>