2

How can I represent double type number in binary form, for example:

dec = 3.14159

to an array consists of its IEEE 754 Double precision binary representation as binaryconvert.com does it

bin = 0100000000001001001000011111100111110000000110111000011001101110

in Matlab? I know there is a similar question, but not for Matlab.

1

2 Answers 2

1

You can do the following:

hexdec = num2hex(dec);
bin = hexToBinaryVector(hexdec);

Example:

hexToBinaryVector(num2hex(pi))
ans =
    1100000000001001001000011111101101010100010001000010110100011000
Sign up to request clarification or add additional context in comments.

1 Comment

FYI, hexToBinaryVector is in the Data Acquisition toolbox. I have most toolboxes with my academic install, but I don't think this one is common unfortunately. Also, the first bit (the sign bit?) seems to be opposite of what is returned by @LuisMendo's answer and the URL in the OP's question.
1

This can be done with typecast(...,'uint8') to get invididual bytes; then fliplr to change byte endianness (at least that's needed on my machine); then converting each byte to binary with dec2base(...,2,8); and finally reshape'ing to a single row:

bin = reshape(dec2base(fliplr(typecast(dec,'uint8')),2,8).',1,[]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.