Convert decimal to binary/octal/hexadecima value and vice versa

25 visualizaciones (últimos 30 días)
TyTy
TyTy el 3 de Dic. de 2018
Comentada: TyTy el 3 de Dic. de 2018
I'm trying to write a program that will convert a decimal number that a user inputs to a binary, octal and hexadecimal value. As well as convert a binary, octal and/or hexadecimal that user inputs to a decimal number. I can not use the functions matlab has to do this conversion. I have to create my own algorithms.I am not skilled in matlab so I do not know how to start or what to do.
Can someone help me
  1 comentario
Walter Roberson
Walter Roberson el 3 de Dic. de 2018
Okay so go ahead and create your own algorithms . Once you have decided on algorithms then start programming in MATLAB. If you run into problems you can post your commented code (that describes your algorithms ) and the error message and we will help you understand the error message. (We might or might not tell you whether your algorithms are correct , depends on our mood. So think about the algorithm .)

Iniciar sesión para comentar.

Respuestas (2)

TyTy
TyTy el 3 de Dic. de 2018
I don't know where to start
  4 comentarios
TyTy
TyTy el 3 de Dic. de 2018
I know how to do the conversions by hand but I do not know how to input them into matlab without using the functions. I watched youtube videos and searched other answers posted on matlab. If you think you know it all show me an example of one of the 8 conversion I need. Belittling me is not helping.
Walter Roberson
Walter Roberson el 3 de Dic. de 2018
How would you convert 13 decimal to binary by hand? How would you convert A5 hex to decimal ?
If you know how to do base conversion by hand then you have a place to start: write down the algorithm and then start converting to MATLAB statements .

Iniciar sesión para comentar.


TyTy
TyTy el 3 de Dic. de 2018
clear all
fprintf (['Hello, What value would you like to convert?\n Type ''1'' for Binary to Decimal OR ''2'' for Hexadecimal to decimal OR''3'' for Octal to decimal OR Type ''4'' for Decimal to Binary OR 5 for Decimal to Hexadecimal OR 6 for Decimal to Octal']);
Value = input('')
if Value = 1
fprintf('What is the value?')
end
this is all i have
  2 comentarios
Walter Roberson
Walter Roberson el 3 de Dic. de 2018
MATLAB uses == for comparisons.
use input() with the 's' option to prompt and accept a value in the form of a character vector .
You might be tempted to accept values as numeric but that will fail for hex because of the A to F characters and will fail for more than 15 binary digits as you start to overflow representation . Process characters on input instead .
Really you only need two routines: one to convert aa character vector in an arbitrary base into numeric form and the other to convert numeric form to arbitrary base . There is no need for distinct routines to handle those bass. With just those two routines you could easily convert base 9 to base 19 for example .
TyTy
TyTy el 3 de Dic. de 2018
How can I loop the code and for my last case, case 5. I can't get the octal to decimal conversion to work
fprintf(['Choose what conversion you would like performed:\n Type 1 for Decimal to' +...
' Binary and Octal\n Type 2 for Decimal to Hexadecimal \n Type 3 for Hexcidecimal to ' +...
'Decimal\n Type 4 for Binary to Decimal\n Type 5 for Octal to Decimal\n']);
choice = input('What is your response:\n ');
if choice ~= 3
if choice ~= 5
decimal = input('What value do you want to convert?\n ');
decimal = num2str(decimal);
lengthtotal = strlength(decimal);
location = strfind(decimal,'.');
n=16;
end
end
switch choice
case 1
fprintf(['Enter the base of the value type' +...
'convert into 2 for binary, or 8 for octal then press Enter\n']);
base = input('Enter Here ');
if location >0
decimalIL = decimal(1:location);
lengthtotalInteger = strlength(decimalIL);
decimalI = str2double(decimalIL);
base = double(base);
s(:,n) = rem(decimalI,base);
while any(decimalI) && n >1
n = n - 1;
decimalI = floor(decimalI/base);
s(:,n) = rem(decimalI,base);
end
c1=1;
decimalb = decimal(location:lengthtotal);
lengthtotalDL = strlength(decimalb);
decimalb = str2num(decimalb);
while any(decimalb) && c1 <= lengthtotalDL
decimalb = decimalb*base;
if decimalb>=1
s1(:,c1) =1;
decimalb = decimalb-1;
else
s1(:,c1) =0;
end
c1 = c1+1;
end
symbols = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
p=size(s);
p1=size(s1);
result = reshape(symbols(s+1),size(s));
result1 = reshape(symbols(s1 + 1),size(s1));
fprintf('The Answer = %s.%s\n\n\n',result,result1);
else
%
decimal = str2double(decimal);
base = double(base);
s(:,n) = rem(decimal,base);
while any(decimal) && n >1
n = n - 1;
decimal = floor(decimal/base);
s(:,n) = rem(decimal,base);
end
symbols = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
p=size(s);
result = reshape(symbols(s + 1),size(s));
fprintf('The conversion result is = %s\n',result);
end
case 2
base = 16;
if location >0
decimalIL = decimal(1:location - 1);
lengthtotalInteger = strlength(decimalIL);
decimalI = str2double(decimalIL);
s(:,n) = rem(decimalI,base);
while any(decimalI) && n >1
n = n - 1;
decimalI = floor(decimalI/base);
s(:,n) = rem(decimalI,base);
end
c1=1;
c2=1;
decimalfrac = decimal(location:lengthtotal);
lengthtotalfrac = strlength(decimalfrac);
decimalfrac = str2num(decimalfrac);
while any(decimalfrac) ~= 0 && c1 ~=8
if decimalfrac>=1
remainder1 = rem(decimalfrac,2);
if remainder1 == 0
decimalbI= decimalfrac;
s1(:,c1) =decimalbI;
else
decimalfrac = num2str(decimalfrac);
lengthtotal = strlength(decimalfrac);
location = strfind(decimalfrac,'.');
decimalbI = decimalfrac(1:location);
decimalbI = str2double(decimalbI);
decimalfrac = decimalfrac(location:lengthtotal);
decimalfrac = str2num(decimalfrac);
decimalfrac = decimalfrac*base;
decimalfrac = num2str(decimalfrac);
lengthtotalDL = strlength(decimalfrac);
decimalfrac = str2num(decimalfrac);
s1(:,c2) =decimalbI;
c2= c2+1;
end
else
decimalfrac = decimalfrac*base;
end
c1 = c1+1;
end
symbols = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
p=size(s);
p1=size(s1);
result = reshape(symbols(s + 1),size(s));
result1 = reshape(symbols(s1 + 1),size(s1));
fprintf('The Conversion Result = %s.%s\n\n\n',result,result1);
conversion();
else
decimal = str2num(decimal);
s(:,n) = rem(decimal,base);
while any(decimal) && n >1
n = n - 1;
decimal = floor(decimal/base);
s(:,n) = rem(decimal,base);
end
symbols = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
p=size(s);
result = reshape(symbols(s + 1),size(s));
% result
fprintf('The Conversion Result = %s\n',result);
end
case 3
decimal = input(['What is the hexadecimal',...
'number you would like to convert. Example ''A''\n ']);
hex = decimal;
hex = upper(hex);
[m,n]=size(hex);
sixteen = 16;
p = fliplr(cumprod([1 sixteen(ones(1,n-1))]));
p = p(ones(m,1),:);
dec = hex <= 64;
hex(dec) = hex(dec) - 48;
dec = hex > 64;
hex(dec) = hex(dec) - 55;
result = sum(hex.*p,2);
case 4
[m,n] = size(decimal);
v = decimal - '0';
twos = pow2(n-1:-1:0);
result = sum(v .* twos(ones(m,1),:),2);
case 5
decimal = input(['What is the value you want to convert from',...
' Octal to Decimal. Example ''111''\n ']);
octal = int2str(decimal);
d = 0;
f = fliplr(octal);
for power = 0 : length(octal) - 1
result = result + str2num(f(power+1)) * 8^power;
end
end

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion 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!

Translated by