fprintf for character above 127 uses 3 bytes
1 次查看(过去 30 天)
显示 更早的评论
Hello Everyone, could somebody give me a hint why apply fprintf to a character beyond 127 would result a consumption of 3 bytes? Is this related to Matlab or the system? Thank you in advance.
Here is my test code, and further below is part of the results I got out of this code:
fid=fopen('testprintbeyond128','w')
for i=1:255
fprintf(fid,'%s\t',num2str(i));
fprintf(fid,'%s\t',dec2hex(i));
cc=fprintf(fid,'%s\t',char(i));
fprintf(fid,'%d\n',cc)
end
fclose(fid)
results:
123 7B { 2
124 7C | 2
125 7D } 2
126 7E ~ 2
127 7F 2
128 80 3
129 81 3
130 82 3
0 个评论
采纳的回答
Guillaume
2016-3-23
Once you've opened your text file, check its encoding with
[~, ~, ~, encoding] = fopen(fid)
You will find that it is UTF-8. In UTF-8 character codes U+0000 between U+007F are encoded on one byte and character codes between U+0080 and U+07FF are encoded on two bytes. Add one byte for the tab (U+0009) and what you're seeing is normal.
3 个评论
Guillaume
2016-3-23
@MarsMat, two things:
If you use fprintf then really you should read the data back with fscanf. This would handle all these encoding issues transparently. While, we're at it, I would open the file with 'wt' and 'rt' to make sure end of lines are handled properly.
To have character from 128 to 255 stored as one byte, you need to explicitly specify an adequate character encoding when you fopen the file. Only you can tell which one is the most appropriate, 'ISO-8859-1' is the closest to unicode but depending on where you're located others may be more appropriate.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!