help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

generating binary numbers


From: Mike Miller
Subject: generating binary numbers
Date: Fri, 19 Jul 2002 17:25:29 -0500 (CDT)

On Fri, 19 Jul 2002, Thomas Becker wrote:

> I want to generate a binary file from octave, i.e writing integer number
> 0 to 255 as binary numbers into a file.
> Can somebody give me a hint how to do this


Well, I can give you a hint.  Use the function dec2bin.  It creates a
binary string from a decimal number.  The tricky part is that it writes a
string which must then be manipulated appropriately for your purposes.
This works for me, but I'd like to hear expert opinions:

for i=0:255, x(i+1,:)=sprintf("%08i",str2num(dec2bin(i))); end

That puts the strings in a matrix called 'x'.  What I'm doing is
converting the binary strings back into integers, then I change them back
into strings with appropriate padding of zeros in front.  It seems kinda
messy but it works.

Mike



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]