Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to get binary representation of a decimal number

Subject: How to get binary representation of a decimal number

From: Wenlong

Date: 30 Nov, 2012 22:43:08

Message: 1 of 2

Hi, all

I am looking for a method that can convert a decimal number to binary vector. I know there are many answers that I can google it, but seems there's nothing fits my problem.

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.

I've found a function ‘dec2bin’, but seems it just convert the integer part, for instance, the result of dec2bin(1.87) is 1.

May I know if there's any functions that can help me?

Thank you very much.

Best regards
Wenlong

Subject: How to get binary representation of a decimal number

From: Roger Stafford

Date: 1 Dec, 2012 02:49:12

Message: 2 of 2

"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

Tags for this Thread

No tags are associated with this thread.

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.

Contact us