getting very weird number from hex2dec()
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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!
0 comentarios
Respuesta aceptada
Walter Roberson
el 20 de En. de 2012
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.
0 comentarios
Más respuestas (1)
William
el 20 de En. de 2012
1 comentario
Walter Roberson
el 20 de En. de 2012
t = dec2hex(sla(1,3)) - '0';
t(t>9) = t(t>9) - 7;
However, if this is your aim, to get the numeric equivalent of each group of 4 bits, you can do that in other ways. For example,
HexPerVal = 2; %hex digits per sla entry
t2 = reshape(dec2bin(sla(:),HexPerVal*4).', 4, []).' - '0';
t3 = reshape(t2(:,1) * 8 + t2(:,2) * 4 + t2(:,3) * 2 + t2(:,4),2,[]).';
t4 = permute(reshape(t3,[HexPerVal,3,5]),[2 3 1]);
Now t4 would be an array whose first two dimensions were the same as sla's dimensions, and whose third dimension would be 1 to the number of hex digits per value (as determined by HexPerval)
Ver también
Categorías
Más información sobre Whos en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!