Hex-to-Binary conversion always outputting 48 or 49

3 views (last 30 days)
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.
  2 Comments
Emerson Butler
Emerson Butler on 14 Aug 2018
I think the problem with mine is that I initialized matrix x as a double by setting it to zeros. It works if I change the initialization to "x = char(zeros(m,n));" and instead of getting the doubles 0, I get the characters 0. Regardless, dpb's solution is much much more efficient and clean.
The only reason I'm initializing is because Matlab is giving me a warning when I don't about the matrix changing size every iteration of the for loop.
dpb
dpb on 14 Aug 2018
" problem with mine is that I initialized matrix x as a double by setting it to zeros"
Ah! Yes, Matlab will do silent type-casting to the target type if at all possible. Part of "there is no free lunch" syndrome in eliminating need for defining a priori comes the fact things can change behind your back w/o knowing it.
I wasn't actually even thinking about the copy being the offending part in the Q? so while I saw it, never paid it "no never mind".
Do note the addenda on error handling and how to treat your alternate return in implementing an "in anger" version of the sample function.

Sign in to comment.

Accepted Answer

dpb
dpb on 13 Aug 2018
Edited: dpb on 14 Aug 2018
"_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'
>>
  3 Comments
dpb
dpb on 14 Aug 2018
Edited: dpb on 14 Aug 2018
Glad to...btw I thought I had added it but see I didn't -- if you really want/need the char() array, that's easy enough to get to as well --
>> cell2mat(BinaryMat)
ans =
3×12 char array
'000100100000'
'101011001000'
'10010110111'
>>
The two features the above function doesn't have that yours does are
  1. The copy of the original -- as written that does have to be "just"(*) a copy so I don't see a real need, just make a direct assignment if there is some actual use for the copy, and
  2. Error checking -- this may be important if it is possible for the input data to be corrupted and contain something other than a valid hex digit. That can be incorporated by doing a comparison test on the input array that all members are in the allowable set (remember to not overlook case). ismember would be one way that also would also return the location(s) of any offending element(s) for reporting.
(*) Actually, that isn't quite true, there's no assignment to the corresponding array location in the output array if there is an offending character; that can be handled as part of the error handling to duplicate functionality if needed. The input validation should happen in the function before the conversion; will have to choose what to do for that location as hex2dec will fail on bad input--
>> hex2dec('X')
Error using hex2dec>hex2decImpl (line 58)
Input to hex2dec should have just 0-9, a-f, or A-F.
Error in hex2dec (line 21)
d = hex2decImpl(h);
>>
Emerson Butler
Emerson Butler on 14 Aug 2018
I don't think I'll need a copy, but error checking will definitely be useful, thanks!

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings 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!