From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How to get binary representation of a decimal number
Date: Sat, 1 Dec 2012 02:49:12 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 6
Message-ID: <k9br78$quk$>
References: <k9bcps$ids$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1354330152 27604 (1 Dec 2012 02:49:12 GMT)
NNTP-Posting-Date: Sat, 1 Dec 2012 02:49:12 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: comp.soft-sys.matlab:783901

"Wenlong" wrote in message <k9bcps$ids$>...
> I need to convert a decimal number (can be both positive or negative, and has a long string after decimal point) into binary. And at some point, I have to convert the binary strings back to decimal numbers.
- - - - - - - -
  If the decimal number is not an integral multiple of a (positive or negative) power of 2, it cannot be converted to a binary fraction without error - that is, the binary fraction would have infinitely many digits.  If you have determined the maximum number of fractional binary digits that you can make use of, just multiply your decimal number by the corresponding power of 2, round it to the nearest integer, convert that to a binary integer, and then shift the binary point back in the binary string by that power.

Roger Stafford