MATLAB: Hex-to-Binary conversion always outputting 48 or 49

binaryconversionfor loophexadecimalif statementstring

Hi, I have a char matrix of hex values that I need to convert to 4-bit binary values. Each entry in the hex matrix is a single letter or number. I have then written some code that takes each entry from the hex matrix and creates 4 entries in the binary matrix.
My problem is that it doesn't work properly, but it did at one point. I must've made some small change that I now can't remember, because the x matrix that should be a copy of HexMat is not a copy, and my BinaryMat consists of 48s and 49s instead of 1s and 0s.
Here's the basic format for my if statements:
if(HexMatrix(coordinates) == 'value'
BinaryMatrix(1) = '1st binary digit corresponding to value';
BinaryMatrix(2) = '2nd binary digit corresponding to value';
BinaryMatrix(3) = '3rd binary digit corresponding to value';
BinaryMatrix(4) = '4th binary digit corresponding to value';
x(coordinates) = value;
Here's the code:
HexMat = ['1','2','0';'A','C','8';'9','6','F']; %Hex Matrix definition
[m,n] = size(HexMat); %My actual matrix can change size based on some dataset which is why I'm not using constants.
%Initialize the binary matrix for speed, and I know it'll be 4 times as
%wide as the hex matrix since I'm using 4-bit binaries.
BinaryMat = zeros(m,n*4);
%I'm also initializing an x matrix which *should* just be a copy of HexMat
x = zeros(m,n);
for w = 1:m
for v = 1:n
if isequal(HexMat(w,v), '0')
BinaryMat(w,v*4-3) = '0';
BinaryMat(w,v*4-2) = '0';
BinaryMat(w,v*4-1) = '0';
BinaryMat(w,v*4) = '0';
x(w,v) = '0';
elseif isequal(HexMat(w,v), '1')
BinaryMat(w,v*4-3) = '0';
BinaryMat(w,v*4-2) = '0';
BinaryMat(w,v*4-1) = '0';
BinaryMat(w,v*4) = '1';
x(w,v) = '1';
elseif isequal(HexMat(w,v), '2')
BinaryMat(w,v*4-3) = '0';
BinaryMat(w,v*4-2) = '0';
BinaryMat(w,v*4-1) = '1';
BinaryMat(w,v*4) = '0';
x(w,v) = '2';
elseif isequal(HexMat(w,v), '3')
BinaryMat(w,v*4-3) = '0';
BinaryMat(w,v*4-2) = '0';
BinaryMat(w,v*4-1) = '1';
BinaryMat(w,v*4) = '1';
x(w,v) = '3';
elseif isequal(HexMat(w,v), '4')
BinaryMat(w,v*4-3) = '0';
BinaryMat(w,v*4-2) = '1';
BinaryMat(w,v*4-1) = '0';
BinaryMat(w,v*4) = '0';
x(w,v) = '4';
elseif isequal(HexMat(w,v), '5')
BinaryMat(w,v*4-3) = '0';
BinaryMat(w,v*4-2) = '1';
BinaryMat(w,v*4-1) = '0';
BinaryMat(w,v*4) = '1';
x(w,v) = '5';
elseif isequal(HexMat(w,v), '6')
BinaryMat(w,v*4-3) = '0';
BinaryMat(w,v*4-2) = '1';
BinaryMat(w,v*4-1) = '1';
BinaryMat(w,v*4) = '0';
x(w,v) = '6';
elseif isequal(HexMat(w,v), '7')
BinaryMat(w,v*4-3) = '0';
BinaryMat(w,v*4-2) = '1';
BinaryMat(w,v*4-1) = '1';
BinaryMat(w,v*4) = '1';
x(w,v) = '7';
elseif isequal(HexMat(w,v), '8')
BinaryMat(w,v*4-3) = '1';
BinaryMat(w,v*4-2) = '0';
BinaryMat(w,v*4-1) = '0';
BinaryMat(w,v*4) = '0';
x(w,v) = '8';
elseif isequal(HexMat(w,v), '9')
BinaryMat(w,v*4-3) = '1';
BinaryMat(w,v*4-2) = '0';
BinaryMat(w,v*4-1) = '0';
BinaryMat(w,v*4) = '1';
x(w,v) = '9';
elseif isequal(HexMat(w,v), 'A')
BinaryMat(w,v*4-3) = '1';
BinaryMat(w,v*4-2) = '0';
BinaryMat(w,v*4-1) = '1';
BinaryMat(w,v*4) = '0';
x(w,v) = 'A';
elseif isequal(HexMat(w,v), 'B')
BinaryMat(w,v*4-3) = '1';
BinaryMat(w,v*4-2) = '0';
BinaryMat(w,v*4-1) = '1';
BinaryMat(w,v*4) = '1';
x(w,v) = 'B';
elseif isequal(HexMat(w,v), 'C')
BinaryMat(w,v*4-3) = '1';
BinaryMat(w,v*4-2) = '1';
BinaryMat(w,v*4-1) = '0';
BinaryMat(w,v*4) = '0';
x(w,v) = 'C';
elseif isequal(HexMat(w,v), 'D')
BinaryMat(w,v*4-3) = '1';
BinaryMat(w,v*4-2) = '1';
BinaryMat(w,v*4-1) = '0';
BinaryMat(w,v*4) = '1';
x(w,v) = 'D';
elseif isequal(HexMat(w,v), "E")
BinaryMat(w,v*4-3) = '1';
BinaryMat(w,v*4-2) = '1';
BinaryMat(w,v*4-1) = '1';
BinaryMat(w,v*4) = '0';
x(w,v) = 'E';
elseif isequal(HexMat(w,v), 'F')
BinaryMat(w,v*4-3) = '1';
BinaryMat(w,v*4-2) = '1';
BinaryMat(w,v*4-1) = '1';
BinaryMat(w,v*4) = '1';
x(w,v) = 'F';
else
w %I want these to display, hence the missing ;
v
er = HexMat(w,v)
error('Hexadecimal mismatch error @ coordinates [w,v] of HexMat, defined as variable er')
end
end
end
The v*4-(3,2,1,0) stuff is because for each entry in HexMat I'll have 4 entries in BinaryMat. My 2nd column in HexMat will correspond to the 2*4-3 (5th), 2*4-2 (6th), 2*4-1 (7th), and 2*4 (8th) columns in BinaryMat. Any help is appreciated, I think I'm overlooking something simple here like some formatting or something. Like I said, it was working earlier today and I thought I only edited it to make comments for clarity, but I must've changed something in the code itself as well.

Best Answer

"_my BinaryMat consists of 48s and 49s instead of 1s and 0s."_
I didn't try to read the code but somewhere you've converted the char return to its internal representation. '48' is the decimal value of the character '0'
>> double('0')
ans =
48
>> char([48;49])
ans =
2×1 char array
'0'
'1'
>>
>> dec2bin(hex2dec('F'))
ans =
'1111'
>> double(ans)
ans =
49 49 49 49
>>
ADDENDUM
I think you can get to useful result quite a lot more simply...see if the following won't serve for your purposes:
function bin=hexmat2bin(HexMat)
% returns cell array of binary bits in input hex array of characters
% output cell array is same shape as input char() array
%

% hexarray=['1','2','0';'A','C','8';'9','6','F'];
% bincell=hexmat2bin(hexarray)
% bincell =
%
% {'0001'} {'0010'} {'0000'}
% {'1010'} {'1100'} {'1000'}
% {'1001'} {'0110'} {'1111'}
[m,n]=size(HexMat);
HexC=mat2cell(HexMat,ones(1,m),ones(1,n));
bin=reshape(cellstr(dec2bin(hex2dec(HexC),4)),m,[]);
end
For your sample input (which I used as the help file example as well) one gets
>> BinaryMat=hexmat2bin(HexMat)
BinaryMat =
3×3 cell array
{'0001'} {'0010'} {'0000'}
{'1010'} {'1100'} {'1000'}
{'1001'} {'0110'} {'1111'}
>>
All you need to do is to dereference the cell array to retrieve any given character string desired--remember that's using the "curlies" instead of ordinary parentheses:
>> BinaryMat{1,2}
ans =
'0010'
>>