How to write ASCII to byte file?
In MATLB versions until R2021a I could write ASCII values stored in a CHAR array into a string as unsigned char by:
data = char([126, 129]); % Exceeds 7 bit ASCII
file = fullfile(tempdir, ‘test.dat’);
[fid, msg] = fopen(file, ‘w’);
assert(fid ~= -1, msg);
fwrite(fid, data); % Write uchar until R2021b
fclose(fid);
This wrote the unsigned chars [126, 129] untill R2021b:
[fid, msg] = fopen(file, ‘r’);
bytes = fread(fid, [1, inf], ‘uint8’); % < R2021b: [126, 129]
Since R2021b the bytes are converted to UTF-8 and the file contains [126, 194, 129], so a specification is required:
fwrite(fid, data, ‘uchar’); % < R2024b: [126, 129], R2024b: [126, 194, 129]
In R2024b this writes [126, 194, 129] again. Intuitively I’ve tried:
fwrite(fid, data, ‘uint8’); % R2024b: [126, 194, 129] also
Playing with the encoding type in fopen does not help also. My questions:
Is there a low level method to write UCHARs stored in a CHAR vector using fopen / fwrite without a Unicode conversion?
This change of behaviour breaks a lot of my codes. Is this considered to be useful?
What is the best way to write unsigned bytes without dependency to the platform?
A workaround is the casting to UINT8:
fwrite(fid, uint8(data), ‘uint8’);
But duplicating data in memory without a reason is a waste of time.In MATLB versions until R2021a I could write ASCII values stored in a CHAR array into a string as unsigned char by:
data = char([126, 129]); % Exceeds 7 bit ASCII
file = fullfile(tempdir, ‘test.dat’);
[fid, msg] = fopen(file, ‘w’);
assert(fid ~= -1, msg);
fwrite(fid, data); % Write uchar until R2021b
fclose(fid);
This wrote the unsigned chars [126, 129] untill R2021b:
[fid, msg] = fopen(file, ‘r’);
bytes = fread(fid, [1, inf], ‘uint8’); % < R2021b: [126, 129]
Since R2021b the bytes are converted to UTF-8 and the file contains [126, 194, 129], so a specification is required:
fwrite(fid, data, ‘uchar’); % < R2024b: [126, 129], R2024b: [126, 194, 129]
In R2024b this writes [126, 194, 129] again. Intuitively I’ve tried:
fwrite(fid, data, ‘uint8’); % R2024b: [126, 194, 129] also
Playing with the encoding type in fopen does not help also. My questions:
Is there a low level method to write UCHARs stored in a CHAR vector using fopen / fwrite without a Unicode conversion?
This change of behaviour breaks a lot of my codes. Is this considered to be useful?
What is the best way to write unsigned bytes without dependency to the platform?
A workaround is the casting to UINT8:
fwrite(fid, uint8(data), ‘uint8’);
But duplicating data in memory without a reason is a waste of time. In MATLB versions until R2021a I could write ASCII values stored in a CHAR array into a string as unsigned char by:
data = char([126, 129]); % Exceeds 7 bit ASCII
file = fullfile(tempdir, ‘test.dat’);
[fid, msg] = fopen(file, ‘w’);
assert(fid ~= -1, msg);
fwrite(fid, data); % Write uchar until R2021b
fclose(fid);
This wrote the unsigned chars [126, 129] untill R2021b:
[fid, msg] = fopen(file, ‘r’);
bytes = fread(fid, [1, inf], ‘uint8’); % < R2021b: [126, 129]
Since R2021b the bytes are converted to UTF-8 and the file contains [126, 194, 129], so a specification is required:
fwrite(fid, data, ‘uchar’); % < R2024b: [126, 129], R2024b: [126, 194, 129]
In R2024b this writes [126, 194, 129] again. Intuitively I’ve tried:
fwrite(fid, data, ‘uint8’); % R2024b: [126, 194, 129] also
Playing with the encoding type in fopen does not help also. My questions:
Is there a low level method to write UCHARs stored in a CHAR vector using fopen / fwrite without a Unicode conversion?
This change of behaviour breaks a lot of my codes. Is this considered to be useful?
What is the best way to write unsigned bytes without dependency to the platform?
A workaround is the casting to UINT8:
fwrite(fid, uint8(data), ‘uint8’);
But duplicating data in memory without a reason is a waste of time. ascii, uchar, fwrite, fopen, unicode MATLAB Answers — New Questions