Wrong print negative number with fprintf in hex
32 views (last 30 days)
Show older comments
vadim onuchin
on 4 May 2021
Commented: Walter Roberson
on 15 Mar 2024
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?
0 Comments
Accepted Answer
Walter Roberson
on 15 Mar 2024
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
2 Comments
Walter Roberson
on 15 Mar 2024
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
More Answers (1)
Swastik Sarkar
on 4 Mar 2024
Hi Vadim,
I understand that you want to print negative hexadecimal number, also I assume the 2’s 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
fprintf("%04x", a + (a<0)*2^32)
Hope this helps.
See Also
Categories
Find more on Logical 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!