8

I have three characters (bigger than 127) and I need to write it in a binary file.
For some reason, MATLAB and PHP/Python tends to write a different characters.
For Python, I have:

s = chr(143)+chr(136);
f = open('pythonOut.txt', 'wb');
f.write(s);
f.close();

For MATLAB, I have:

s = strcat(char(143),char(136));
fid = fopen('matlabOut.txt');
fwrite(fid, s, 'char');
fclose(fid);

When I compare these two files, they're different. (using diff and/or cmp command).
More over, when I do

disp(char(hex2dec('88'))) //MATLAB prints 
print chr(0x88) //PYTHON prints ˆ

Both outputs are different. I want to make my MATLAB code same as Python. What's wrong with MATLAB code?

  • 1
    Just to help the readers, can you show the binary contents of the files? Also, why do you assume it's the MATLAB code that is wrong? – Eitan T Jan 17 '13 at 09:56
  • 1
    i think it's just a typo problem in the code. 143,134 and 143,136. of course they're different! you made them different. – thang Jan 17 '13 at 10:10
  • @thang Thanks, thang! That was a typo and I just fixed that. – user1983388 Jan 17 '13 at 10:28

2 Answers2

4

You're trying to display extended ASCII characters, i.e symbols with an ASCII number greater than 128. MATLAB does not use extended ASCII internally, it uses 16-bit Unicode instead.

If you want to write the same values as in the Python script, use native2unicode to obtain the characters you want. For example, native2unicode(136) returns ^.

Eitan T
  • 32,660
  • 14
  • 72
  • 109
4

The fact that the two files are different seems obvious; chr(134) is obviously different from char(136) :)

In Matlab, only the first 127 characters correspond to (non-extended) ASCII; anything after that is Unicode16.

In Python, the first 255 characters correspond to (extended) ASCII (use unichr() for Unicode).

However, unicode 0x88 is the same as the extended ASCII 0x88 (as are most of the others). The reason Matlab does not display it correctly, is due to the fact that the Matlab command window does not treat Unicode very well by default, while Python (running in a terminal or so I presume) usually does a better job.

Try changing the font in Matlab's command window, or starting Matlab in a terminal and print the 0x88 character; it should be the same.

In any case, the output of the characters to file should not result in any difference; it is just a display issue.

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96