Hexadecimal to Binary Conversion in MATLAB
Advertisement
This page provides MATLAB source code for converting hexadecimal values to binary.
% function [binary_op] = hex2binyb(data)
hexa='aa83d32'
data=hexa;
% function [binary_op] = hex2binyb(data)
bin = [0 0 0 0; 0 0 0 1; 0 0 1 0; 0 0 1 1; 0 1 0 0; 0 1 0 1; 0 1 1 0; 0 1 1 1; 1 0 0 0; 1 0 0 1; 1 0 1 0; 1 0 1 1; 1 1 0 0; 1 1 0 1; 1 1 1 0; 1 1 1 1];
hex = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
data = lower(data);
data1=length(data);
binary_op = [];
for p = 1:data1
index(p) = find(hex == data(p));
binary_op = [binary_op bin(index(p),:)];
end
Input and Output Example
Given the hexadecimal input:
hexa = aa83d32
binary_op =
Columns 1 through 17
1 0 1 0 1 0 1 0 1 0 0 0 0 0 1 1 1
Columns 18 through 28
1 0 1 0 0 1 1 0 0 1 0