DECIMAL To Binary Representation
显示 更早的评论
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 个评论
Star Strider
2018-1-28
‘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
2018-1-28
Star Strider
2018-1-28
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
2018-1-28
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
2018-1-28
Star Strider
2018-1-28
Mine only works with positive integers, so I will stop here.
Dereje
2018-1-28
Walter Roberson
2018-1-28
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
2018-1-28
John D'Errico
2018-1-28
I suppose I need to post num2bin sometime. Too many round-tuits.
回答(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
类别
在 帮助中心 和 File Exchange 中查找有关 Numeric Types 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!