Path: news.mathworks.com!not-for-mail 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$1@newscl01ah.mathworks.com> References: <k9bcps$ids$1@newscl01ah.mathworks.com> Reply-To: <HIDDEN> NNTP-Posting-Host: www-03-blr.mathworks.com Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: newscl01ah.mathworks.com 1354330152 27604 172.30.248.48 (1 Dec 2012 02:49:12 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Sat, 1 Dec 2012 02:49:12 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 1187260 Xref: news.mathworks.com comp.soft-sys.matlab:783901 "Wenlong" wrote in message <k9bcps$ids$1@newscl01ah.mathworks.com>... > 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