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 个评论

‘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.)
The only thing that I've done so far is that the code that converts any numbers(not fraction) in to binary not ASCII representation. And this is why I said it help.
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.
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?
The input is any decimal numbers(Either +ve or -ve integers) and as you mentioned it represents a floating point number.
Mine only works with positive integers, so I will stop here.
May be Why not you add
if s(s<0)
s=-s
end
and it sees it as a +ve number.
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.
Of course if it is -ve integer, then the code considers it as a +ve integer but for me I know that my input is -ve and I considers it as -ve. For example if my input is
24
Then the out put is
'11000'
In the same way for -24 is also
'11000'
Since the input is -ve value it is clear that you must add -ve to get the correct result. I haven't got a result for fractions.
I suppose I need to post num2bin sometime. Too many round-tuits.

请先登录,再进行评论。

回答(1 个)

Stephen23
Stephen23 2018-1-28
编辑:Stephen23 2018-1-28
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 的更多信息

标签

提问:

2018-1-28

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by