converting binary to hexadecimal
Show older comments
bin_str = input('Enter binary number: ', 's');
i = length(bin_str);
disp(i);
n = ceil(i/4);
disp(n);
for g = n : -1 : 1
if i > 4
hex_str(g) = b2h(bin_str(i-3 : i));
i = i - 4;
else
hex_str(g) = b2h(bin_str(1 : i));
end
end
function h = b2h(b)
switch b
case {'0', '00', '000', '0000'}
h = '0';
case {'1', '01', '001', '0001'}
h = '1';
case {'10', '010', '0010'}
h = '2';
case {'11', '011', '0011'}
h = '3';
case {'100', '0100'}
h = '4';
case {'101', '0101'}
h = '5';
case {'110', '0110'}
h = '6';
case {'111', '0111'}
h = '7';
case '1000'
h = '8';
case '1001'
h = '9';
case '1010'
h = 'A';
case '1011'
h = 'B';
case '1100'
h = 'C';
case '1101'
h = 'D';
case '1110'
h = 'E';
case '1111'
h = 'F';
end
I am trying to take the binary input and convert it into hexadecimal value,but im getting the following error:
??? Error: File: filename.m Line: 19 Column: 1 Function definitions are not permitted in this context
I can't understand this error.. What changes should i need to make in order to get the correct output?
Pls help me! Thank you
Accepted Answer
More Answers (1)
Andrei Bobrov
on 29 Sep 2015
b = {'1100100101111111101';'1100000011001'};
out = dec2hex(bin2dec(b));
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!