Logo Passei Direto
Buscar

Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original

%% NÚMERO DECIMAL PARA HEXADECIMAL
clear all; clc; format compact;
numero_decimal = input('Digite um número no sistema de base 10: ');
numero = numero_decimal;
numero_decimal = num2str(numero_decimal);
for i=1:length(numero_decimal)
 divisao = floor(numero/16);
 vetor(i) = mod(numero, 16); 
 numero = divisao;
 if divisao < 1
 break
 end
end
numero_hexa = fliplr(vetor);
tam = length(numero_hexa);
for i=1:tam
 if numero_hexa(i) == 10
 aux(i) = 'A';
 elseif numero_hexa(i) == 11
 aux(i) = 'B';
 elseif numero_hexa(i) == 12
 aux(i) = 'C';
 elseif numero_hexa(i) == 13
 aux(i) = 'D';
 elseif numero_hexa(i) == 14
 aux(i) = 'E';
 elseif numero_hexa(i) == 15
 aux(i) = 'F';
 else
 aux(i) = num2str(numero_hexa(i));
 end
end
aux
%% NÚMERO HEXADECIMAL PARA DECIMAL
home; clear all;
hex = input('Digite um número em hexadecimal: ', 's');
for i=1:length(hex)
 aux(i) = sscanf(hex(i), '%x')*16^(length(hex)-(i));
end
fprintf('Hexadecimal = %s\nDecimal = %d\n', hex, sum(aux));
%% NÚMERO HEXADECIMAL PARA BINÁRIO
hexa = input('Digite um número em hexadecimal: ', 's');
hexa = upper(hexa);
tam = length(hexa);
resultado = ' ';
for i=1:tam
 if hexa(i) == 'A'
 aux = '1010';
 resultado = strcat(resultado, aux);
 elseif hexa(i) == 'B'
 aux = '1011';
 resultado = strcat(resultado, aux);
 elseif hexa(i) == 'C'
 aux = '1100';
 resultado = strcat(resultado, aux);
 elseif hexa(i) == 'D'
 aux = '1101';
 resultado = strcat(resultado, aux);
 elseif hexa(i) == 'E'
 aux = '1110';
 resultado = strcat(resultado, aux);
 elseif hexa(i) == 'F'
 aux = '1111';
 resultado = strcat(resultado, aux);
 elseif hexa(i) == '0'
 aux = dec2bin(0, 4);
 resultado = strcat(resultado, aux);
 elseif hexa(i) == '1'
 aux = dec2bin(1, 4);
 resultado = strcat(resultado, aux);
 elseif hexa(i) == '2'
 aux = dec2bin(2, 4);
 resultado = strcat(resultado, aux);
 elseif hexa(i) == '3'
 aux = dec2bin(3, 4);
 resultado = strcat(resultado, aux);
 elseif hexa(i) == '4'
 aux = dec2bin(4, 4);
 resultado = strcat(resultado, aux);
 elseif hexa(i) == '5'
 aux = dec2bin(5, 4);
 resultado = strcat(resultado, aux);
 elseif hexa(i) == '6'
 aux = dec2bin(6, 4);
 resultado = strcat(resultado, aux);
 elseif hexa(i) == '7'
 aux = dec2bin(7, 4);
 resultado = strcat(resultado, aux);
 elseif hexa(i) == '8'
 aux = dec2bin(8, 4);
 resultado = strcat(resultado, aux);
 elseif hexa(i) == '9'
 aux = dec2bin(9, 4);
 resultado = strcat(resultado, aux);
 end
end
resultado
%% NÚMERO BINÁRIO PARA HEXADECIMAL
clear all; home;
bin = input('Digite um Número em Binário: ', 's');
tam = length(bin);
if mod(tam, 4) ~= 0
 z = zeros(1, 4 - mod(tam, 4));
 z = num2str(z);
 z = deblankl(z);
 bin = strcat(z, bin);
end
tam = length(bin);
for i = 1:(tam/4)
 for j = 1:4
 aux(j) = bin(4*(i - 1) + j)
 end
 aux2 = aux;
 aux2 = bin2dec(aux2);
 if aux2 > 9
 if aux2 == 10
 hexa(i) = 'A';
 elseif aux2 == 11
 hexa(i) = 'B';
 elseif aux2 == 12
 hexa(i) = 'C';
 elseif aux2 == 13
 hexa(i) = 'D';
 elseif aux2 == 14
 hexa(i) = 'E';
 elseif aux2 == 15
 hexa(i) = 'F';
 end
 else
 hexa(i) = num2str(aux2);
 end
end
hexa

Teste o Premium para desbloquear

Aproveite todos os benefícios por 3 dias sem pagar! 😉
Já tem cadastro?