Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Converting decimal to binary

Subject: Converting decimal to binary

From: Christopher

Date: 28 Mar, 2008 04:43:32

Message: 1 of 10

How do i convert these eigen-coefficients to binary in matlab?

688.192179500702
1.54987134237672e-13
5.13126867385250e-14
1.54543045027822e-13
-8.27671264858054e-14
2.44387843295613e-14
1.83464354819307e-14
-1.45383705074664e-13
1.33448807559944e-13
-2.48412401759879e-14

is there a function for it?
Thanks

Subject: Converting decimal to binary

From: Daniel Armyr

Date: 28 Mar, 2008 07:52:01

Message: 2 of 10

Do you want to find the binary representation of the data in
ieee-754 format, or do you want to find the binary
representation of the actual numerical values?

The first is not possible within matlab, although you of
course can write a function yourself to do it (The
specification is available on Wikipedia. It isn't difficult.
I have done it)

The second would have been possible, had the numbers been
integers (dec2bin), but now you have to do it yourself as
well. But it is easy to do. Start by finding the highest bit
that fits inside the number, then subtract 2^(whatever the
bit was). Rinse and repeat.

--DA

Subject: Converting decimal to binary

From: Christopher Ngau

Date: 28 Mar, 2008 08:58:02

Message: 3 of 10

"Daniel Armyr" <firstname@lastname.se> wrote in message
<fsi831$7e5$1@fred.mathworks.com>...
> Do you want to find the binary representation of the data in
> ieee-754 format, or do you want to find the binary
> representation of the actual numerical values?
>
> The first is not possible within matlab, although you of
> course can write a function yourself to do it (The
> specification is available on Wikipedia. It isn't difficult.
> I have done it)
>
> The second would have been possible, had the numbers been
> integers (dec2bin), but now you have to do it yourself as
> well. But it is easy to do. Start by finding the highest bit
> that fits inside the number, then subtract 2^(whatever the
> bit was). Rinse and repeat.
>
> --DA
>

I'm not really sure about the second method, but as far as i
know, if decimal fraction will result in a decimal place in
the binary format, example, 0.101001.

I'm trying to implement these values as 1s and 0s, and i
don't see any way of representing the notation ".".

Subject: Converting decimal to binary

From: Randy Poe

Date: 28 Mar, 2008 10:22:04

Message: 4 of 10

On Mar 28, 4:58=A0am, "Christopher Ngau" <chris...@msn.com> wrote:
> "Daniel Armyr" <firstn...@lastname.se> wrote in message
>
> <fsi831$7e...@fred.mathworks.com>...
>
>
>
> > Do you want to find the binary representation of the data in
> > ieee-754 format, or do you want to find the binary
> > representation of the actual numerical values?
>
> > The first is not possible within matlab, although you of
> > course can write a function yourself to do it (The
> > specification is available on Wikipedia. It isn't difficult.
> > I have done it)
>
> > The second would have been possible, had the numbers been
> > integers (dec2bin), but now you have to do it yourself as
> > well. But it is easy to do. Start by finding the highest bit
> > that fits inside the number, then subtract 2^(whatever the
> > bit was). Rinse and repeat.
>
> > --DA
>
> I'm not really sure about the second method, but as far as i
> know, if decimal fraction will result in a decimal place in
> the binary format, example, 0.101001.

Internally, they are represented in scientific notation
in binary. So if you were to actually decode the IEEE-754
notation, you would already have your number in the
form 1.xxxxx * 2^yyyy

Is that what you want?

>
> I'm trying to implement these values as 1s and 0s, and i
> don't see any way of representing the notation ".".

I don't understand what you mean. If you want a
decimal point, you just write one.

If you found out a number was
    1.01011101 * 2^-7
what difficulty would you have in "representing
the notation '.'"?

Most of your numbers are on the order of 10^-13, which
is about 2^-43, so if you didn't want to use scientific notation
you'd have to precede them with 0.00... (43 zeros).

                 - Randy

Subject: Converting decimal to binary

From: Christopher Ngau

Date: 28 Mar, 2008 10:53:03

Message: 5 of 10

Randy Poe <poespam-trap@yahoo.com> wrote in message
<ba43f950-ab12-4e64-a020-4c21827034ce@n58g2000hsf.googlegroups.com>...
> On Mar 28, 4:58=A0am, "Christopher Ngau"
<chris...@msn.com> wrote:
> > "Daniel Armyr" <firstn...@lastname.se> wrote in message
> >
> > <fsi831$7e...@fred.mathworks.com>...
> >
> >
> >
> > > Do you want to find the binary representation of the
data in
> > > ieee-754 format, or do you want to find the binary
> > > representation of the actual numerical values?
> >
> > > The first is not possible within matlab, although you of
> > > course can write a function yourself to do it (The
> > > specification is available on Wikipedia. It isn't
difficult.
> > > I have done it)
> >
> > > The second would have been possible, had the numbers been
> > > integers (dec2bin), but now you have to do it yourself as
> > > well. But it is easy to do. Start by finding the
highest bit
> > > that fits inside the number, then subtract 2^(whatever the
> > > bit was). Rinse and repeat.
> >
> > > --DA
> >
> > I'm not really sure about the second method, but as far as i
> > know, if decimal fraction will result in a decimal place in
> > the binary format, example, 0.101001.
>
> Internally, they are represented in scientific notation
> in binary. So if you were to actually decode the IEEE-754
> notation, you would already have your number in the
> form 1.xxxxx * 2^yyyy
>
> Is that what you want?
>
> >
> > I'm trying to implement these values as 1s and 0s, and i
> > don't see any way of representing the notation ".".
>
> I don't understand what you mean. If you want a
> decimal point, you just write one.
>
> If you found out a number was
> 1.01011101 * 2^-7
> what difficulty would you have in "representing
> the notation '.'"?
>
> Most of your numbers are on the order of 10^-13, which
> is about 2^-43, so if you didn't want to use scientific
notation
> you'd have to precede them with 0.00... (43 zeros).
>
> - Randy
>
sorry for the clarify of my question. Actually i'm trying to
represent my eigen coefficients as in 4 bytes per coefficient.

Subject: Converting decimal to binary

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 28 Mar, 2008 16:23:46

Message: 6 of 10

In article <fsiimf$rg3$1@fred.mathworks.com>,
Christopher Ngau <chrisnwh@msn.com> wrote:

>sorry for the clarify of my question. Actually i'm trying to
>represent my eigen coefficients as in 4 bytes per coefficient.

single(number) will convert the number to IEEE754 single precision
(4 bytes) provided it is within range.

--
  "Eightly percent of the people in the world are fools and the
  rest of us are in danger of contamination." -- Walter Matthau

Subject: Converting decimal to binary

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 28 Mar, 2008 16:31:30

Message: 7 of 10

In article <fsi831$7e5$1@fred.mathworks.com>,
Daniel Armyr <firstname@lastname.se> wrote:
>Do you want to find the binary representation of the data in
>ieee-754 format, or do you want to find the binary
>representation of the actual numerical values?

>The first is not possible within matlab

sprintf( '%0.16X', typecast(number,'uint64') )
--
  "The whole history of civilization is strewn with creeds and
  institutions which were invaluable at first, and deadly
  afterwards." -- Walter Bagehot

Subject: Converting decimal to binary

From: Roger Stafford

Date: 28 Mar, 2008 20:47:02

Message: 8 of 10

Christopher <chrisnwh@msn.com> wrote in message
<13606225.1206679443088.JavaMail.jakarta@nitrogen.mathforum.org>...
> How do i convert these eigen-coefficients to binary in matlab?
>
> 688.192179500702
> 1.54987134237672e-13
> 5.13126867385250e-14
> 1.54543045027822e-13
> -8.27671264858054e-14
> 2.44387843295613e-14
> 1.83464354819307e-14
> -1.45383705074664e-13
> 1.33448807559944e-13
> -2.48412401759879e-14
>
> is there a function for it?
> Thanks
-------------
  This is a function I wrote for my own edification. Perhaps you can make use
of it. It converts a single scalar 'double' to a string of 53 binary digits,
including a binary (decimal) point, a sign, and an exponent of 2. This
representation is precise, giving exactly what is contained in the number as
stored in its internal IEEE 754 format.

%%%%%%%%%%%%%%%%%%%%%
function s = binstr(x)

if ~finite(x)|(length(x)~=1), error('x must be a finite scalar.'),end
b = (x<0); x = abs(x);
s = zeros(1,53);
[f,e] = log2(x);
for i = 1:53
 f = 2*f;
 d = floor(f);
 f = f - d;
 s(i) = d+48;
end
s = ['0.' s sprintf('*2^(%d)',e)];
if b, s = ['-' s]; end
s = char(s);
return
%%%%%%%%%%%%%%%%%%%%%

  Second comment: Nine of those ten eigenvalues look to me as those they
are values that would have been precisely zero except for round-off errors in
the eigenvector routine that produced them. Converting each of these nine
to binary, results in a bit string which, with the addition or subtraction of only
a small amount at the least significant bit end, are strings of only a short
length representing very small quantities followed by trailing zeros. (Very
likely those needed least significant bit end adjustments are primarily due to
the inherent imprecision of the decimal strings you have given above.)

Roger Stafford

Subject: Converting decimal to binary

From: Derek O'Connor

Date: 28 Mar, 2008 23:09:18

Message: 9 of 10

Christopher <chrisnwh@msn.com> wrote in message
<13606225.1206679443088.JavaMail.jakarta@nitrogen.mathforum.org>...
> How do i convert these eigen-coefficients to binary in matlab?
>
> 688.192179500702
> 1.54987134237672e-13
> 5.13126867385250e-14
> 1.54543045027822e-13
> -8.27671264858054e-14
> 2.44387843295613e-14
> 1.83464354819307e-14
> -1.45383705074664e-13
> 1.33448807559944e-13
> -2.48412401759879e-14
>
> is there a function for it?
> Thanks

Search for 'num2bin' by Herman Gollwitzer on Matlab File
Exchange. The version I have is dated Oct 7, 2000, but there
may be an updated version.

Derek O'Connor


Subject: Converting decimal to binary

From: Daniel Armyr

Date: 8 Apr, 2008 07:07:01

Message: 10 of 10

> >The first is not possible within matlab
> sprintf( '%0.16X', typecast(number,'uint64') )
I stand corrected. It has been a mystery to me that Matlab
couldn't do this, but I never found the typecast function.
Thank you.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics