How to convert decimal values to a fix length of binary numbers?

20 vues (au cours des 30 derniers jours)
Afaq Ahmad
Afaq Ahmad le 18 Jan 2021
I have an image of size=500*500*3 consisting of 750,000 elements in decimal values ranging from 0-255. To be honest, I am quite confused myself, I am having trouble understanding the concept. So please correct If I am wrong in interpreting something.
I want to convert every deciamal value to a fix 10-length binary value. The binary value of 255 is 11111111. I want every value to be converted into 10-length binary value.
Like, 255 should be 0011111111, not 11111111 and 9 should be 00001001 rather 1001. So as a whole, I will get 750,000*10 binary values of the original image.
Also, If I convert this 750,000*10 image back into image of 750,000 elements (original values), by converting those 10-length binary numbers into their respective decimal values, I want the orignal image rather some garbage image.
I have been working on it for a few days, but nothing comes up.
  1 commentaire
James Tursa
James Tursa le 18 Jan 2021
Modifié(e) : James Tursa le 18 Jan 2021
There is no 10-bit native integer in MATLAB. Are you asking about something from the Fixed Point Toolbox? Or each number converted to a 10 character string? What is the class of the variable you want for an output of this conversion? Why do you need 10 bits? Is this for export into a file to be read by another program, or ...?

Connectez-vous pour commenter.

Réponse acceptée

Masoud Dorvash
Masoud Dorvash le 19 Jan 2021
Modifié(e) : Masoud Dorvash le 19 Jan 2021
Hi there,
you can easily use
dec2bin(n,p)
where n is your decimal number and p is your desired length (for your question is 10)
dec2bin(9,10)
ans =
'0000001001'
but you need to becareful that this is not a number (it's string), if you want to convert it to a number you need too use this one instead
dec2bin(9,10)-'0'
hope this works for you.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Produits


Version

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by