Converting decimal to binary and vise versa
    5 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    John Dimopoulos
 el 9 de En. de 2018
  
    
    
    
    
    Comentada: John Dimopoulos
 el 10 de En. de 2018
            Hello Matlab Community,
I have a problem. I want to create a function that converts the decimal POSITIVE number that the user gives(maximum number that the user can give is 255) to binary(8-bit accuracy for every number from 0 to 255) and also another function that takes a binary number (max: 11111111 = 255) and converts it to decimal. I'd like to use the ''for'' loop and not functions of matlab like ''dec2bin'' or ''bin2dec''.I've tried a lot but still something doesn't work.
Thanks in Advance
Respuesta aceptada
  Roger Stafford
      
      
 el 9 de En. de 2018
        I’ll assume that the form of the binary version is a row vector, B, of eight ones or zeros, and that the "decimal" number is a double precision floating point number, D, whose value is an integer ranging from 0 to 255.
From D to B:
B = zeros(1,8);
for k = 8:-1:1
  B(k) = mod(D,2);
  D = (D-B(k))/2;
end
From B to D:
D = sum(B.*(2.^(7:-1:0)));
Más respuestas (0)
Ver también
Categorías
				Más información sobre Axes Appearance en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!