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!