DECIMAL To Binary Representation
Show older comments
Need help for a code that changes numbers into binary normal representation and storage in a single precision. Not MATLAB built-in converter. Thanks
10 Comments
Star Strider
on 28 Jan 2018
‘Help’ means to us that you already have code, and you may have problems getting it to work.
Show us what you have done, and we will help you to get it to work. (I have already coded a version that gives a correct result, so you can as well.)
Dereje
on 28 Jan 2018
Star Strider
on 28 Jan 2018
My code does the same with decimal integers, since I do not want to do the whole IEEE 754 floating-point representation. (Mine also produces a binary character array.)
Please post what you have done. Tell us what is not working, and include (copy) the complete red text of any error your code throws, and paste it to a Comment here.
Walter Roberson
on 28 Jan 2018
What is the format of your input? Is it a character vector in which each character is either '0' or '1' ? Is the input a decimal number such as 10011 in decimal form but intended to convey binary 1 0 0 1 1 (decimal 19) ? Is there possibly a '.' character somewhere in the input? Is the input intended to represent an integer? Is the input intended to represent a floating point number? How are negative numbers indicated?
Dereje
on 28 Jan 2018
Star Strider
on 28 Jan 2018
Mine only works with positive integers, so I will stop here.
Dereje
on 28 Jan 2018
Walter Roberson
on 28 Jan 2018
Please give us a couple of examples of the input and desired output, including at least one negative, one integer, and one with fractional portion.
If you take the negative of negative values before converting then you cannot tell from the output whether the input was negative or not, so converting back could not give you the same result as the original.
Dereje
on 28 Jan 2018
John D'Errico
on 28 Jan 2018
I suppose I need to post num2bin sometime. Too many round-tuits.
Answers (1)
Where N is an integer:
>> N = -24;
>> V = pow2(1+fix(log2(abs(N))):-1:0);
>> Z = fix(mod(abs(N),V(1:end-1))./V(2:end)) % numeric vector
Z =
1 1 0 0 0
>> char(Z+'0') % char vector
ans = 11000
Categories
Find more on Numeric Types in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!