Adjusting size of matrix when converting base 10 to binary

1 vue (au cours des 30 derniers jours)
Samuel Brewton
Samuel Brewton le 16 Fév 2021
Modifié(e) : Stephen23 le 16 Fév 2021
I'm looking to convert a number (0-255) into binary and then put that into a 1x8 matrix returning 1 and 0's. The issue I'm having is fitting the vectors together since a number like 30 gives [1 1 1 1 0] rather than [0 0 0 1 1 1 1 0]. I'd prefer not to manually add in the first rows each time I get to the next 2^n.
Any help is appreciated.
NumBin = num2str(dec2bin(30)) - '0';
%Returns 1 1 1 1 0
NumBin = num2str(dec2bin(255)) - '0';
%Returns 1 1 1 1 1 1 1 1
%Looking for:
% 0 0 0 1 1 1 1 0
% 1 1 1 1 1 1 1 1

Réponse acceptée

James Tursa
James Tursa le 16 Fév 2021
Specify the number of binary digits to use. E.g.,
dec2bin(30,8)
  1 commentaire
Samuel Brewton
Samuel Brewton le 16 Fév 2021
Modifié(e) : Stephen23 le 16 Fév 2021
Works perfectly! Thank you.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

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

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by