Wrong print negative number with fprintf in hex

33 Ansichten (letzte 30 Tage)
vadim onuchin
vadim onuchin am 4 Mai 2021
Kommentiert: Walter Roberson am 15 Mär. 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?

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 15 Mär. 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
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 Kommentare
vadim onuchin
vadim onuchin am 15 Mär. 2024
Bearbeitet: vadim onuchin am 15 Mär. 2024
yes, in newer version it's working. but in matlab r2019b not working.
It's bug in matlab r2019b.
Walter Roberson
Walter Roberson am 15 Mär. 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Swastik Sarkar
Swastik Sarkar am 4 Mär. 2024
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.
  1 Kommentar
vadim onuchin
vadim onuchin am 15 Mär. 2024
Bearbeitet: vadim onuchin am 15 Mär. 2024
I want to print int16 numbers. Your example working for double numbers.

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by