Wrong print negative number with fprintf in hex

23 次查看(过去 30 天)
Why can't print function fprintf negative values in hex?
For example:
rng(73);
N = 20;
r = (randn(N, 1) + 1j.*randn(N, 1)) * 10000;
for i = 1:N
fprintf('Real[%d] = %04X; ', i, int16(real(r(i))));
fprintf('Imag[%d] = %04X; \n', i, int16(imag(r(i))));
end
And result:
Real[1] = 0D6C; Imag[1] = 1DF2;
Real[2] = 053A; Imag[2] = -1.007700e+04;
Real[3] = 00E1; Imag[3] = -1.237000e+04;
Real[4] = 0D85; Imag[4] = 0435;
Real[5] = -1.700000e+02; Imag[5] = -1.814200e+04;
Real[6] = -4.073000e+03; Imag[6] = -5.949000e+03;
Real[7] = -1.211600e+04; Imag[7] = -1.676000e+04;
Real[8] = 0875; Imag[8] = -5.985000e+03;
Real[9] = 3021; Imag[9] = 19A0;
Real[10] = -1.096100e+04; Imag[10] = 378C;
Real[11] = -3.266000e+03; Imag[11] = 20AB;
Real[12] = 0B82; Imag[12] = 167C;
Real[13] = -1.231200e+04; Imag[13] = -1.007300e+04;
Real[14] = 270D; Imag[14] = 091E;
Real[15] = -1.406600e+04; Imag[15] = 13FC;
Real[16] = 056C; Imag[16] = 0E25;
Real[17] = -6.861000e+03; Imag[17] = -1.780700e+04;
Real[18] = 1EE4; Imag[18] = -6.337000e+03;
Real[19] = 1EF1; Imag[19] = 01D4;
Real[20] = -1.694700e+04; Imag[20] = -8.530000e+02;
May be some cast need before?

采纳的回答

Walter Roberson
Walter Roberson 2024-3-15
rng(73);
N = 20;
r = (randn(N, 1) + 1j.*randn(N, 1)) * 10000;
for i = 1:N
fprintf('Real[%d] = %04X; ', i, typecast(int16(real(r(i))), 'uint16')); fprintf('Imag[%d] = %04X; \n', i, typecast(int16(imag(r(i))), 'uint16'));
end
Real[1] = 0D6C; Imag[1] = 1DF2; Real[2] = 053A; Imag[2] = D8A3; Real[3] = 00E1; Imag[3] = CFAE; Real[4] = 0D85; Imag[4] = 0435; Real[5] = FF56; Imag[5] = B922; Real[6] = F017; Imag[6] = E8C3; Real[7] = D0AC; Imag[7] = BE88; Real[8] = 0875; Imag[8] = E89F; Real[9] = 3021; Imag[9] = 19A0; Real[10] = D52F; Imag[10] = 378C; Real[11] = F33E; Imag[11] = 20AB; Real[12] = 0B82; Imag[12] = 167C; Real[13] = CFE8; Imag[13] = D8A7; Real[14] = 270D; Imag[14] = 091E; Real[15] = C90E; Imag[15] = 13FC; Real[16] = 056C; Imag[16] = 0E25; Real[17] = E533; Imag[17] = BA71; Real[18] = 1EE4; Imag[18] = E73F; Real[19] = 1EF1; Imag[19] = 01D4; Real[20] = BDCD; Imag[20] = FCAB;
  2 个评论
vadim onuchin
vadim onuchin 2024-3-15
编辑:vadim onuchin 2024-3-15
yes, in newer version it's working. but in matlab r2019b not working.
It's bug in matlab r2019b.
Walter Roberson
Walter Roberson 2024-3-15
I installed and tested R2019b. Given the above code, it produces exactly the same output as abover.
It is not a bug that %x does not handle negative values; %x only handles positive values. Using typecast() as I show here works

请先登录,再进行评论。

更多回答(1 个)

Swastik
Swastik 2024-3-4
Hi Vadim,
I understand that you want to print negative hexadecimal number, also I assume the 2s complement of the negative number’s absolute value is to be printed in hex. So, -1 would print as 0xFFFF according to this.
I found that fprintf in MATLAB does not print negative numbers with the hexadecimal format(%x), this is mentioned in the documentation of fprintf which reads:
If you specify a conversion that does not fit the data, such as a text conversion for a numeric value, MATLAB overrides the specified conversion, and uses %e.
Here is the link for the same:
In this case, MATLAB expects an unsigned integer to the format specifier “%x”, however does not find one and defaults to “%e”.
I would like to suggest the following workaround to print the hexadecimal representation of a negative number,
If a is a signed integer, then we can apply the following operation to make sure it prints in hexadecimal every time.
a = -1
a = -1
fprintf("%04x", a + (a<0)*2^32)
ffffffff
Hope this helps.

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

产品


版本

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by