DECIMAL To Binary Representation
Mostrar comentarios más antiguos
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 comentarios
Star Strider
el 28 de En. de 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
el 28 de En. de 2018
Star Strider
el 28 de En. de 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
el 28 de En. de 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
el 28 de En. de 2018
Star Strider
el 28 de En. de 2018
Mine only works with positive integers, so I will stop here.
Dereje
el 28 de En. de 2018
Walter Roberson
el 28 de En. de 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
el 28 de En. de 2018
John D'Errico
el 28 de En. de 2018
I suppose I need to post num2bin sometime. Too many round-tuits.
Respuestas (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
Categorías
Más información sobre Numeric Types en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!