MATLAB: Getting very weird number from hex2dec()

doublehex2dec

Hello,
I am attempting to convert large matrices of doubles to hex. I have an S matrix [2048 x 984] full of zeros
If I do this
>> dec2hex(s1a(1,3))
ans =
0
which is what should happen. However If I do this…..
HexS1a = ones(2048,984);
for aa = 1:984
for bb = 1:2048
HexS1a(bb,aa) = HexS1a(bb,aa)*dec2hex(s1a(bb,aa));
end
end
I get a matrix of "48"'s not zeros. The type is double. Can anyone explain this? It's driving me nuts!

Best Answer

dec2hex(sla(1,3)) is not producing a digit zero: it is producing the string '0' . The numeric code for the character '0' happens to be 48. char(48) = '0'
ones(2048,984) is a double precision datatype. When you store a character to a double precision data type, the character gets converted to its numeric code.
If you are planning to multiply hex digits then you will need something more sophisticated that the "*" operator.