필터 지우기
필터 지우기

fprintf save file as UINT16 or UTF16

조회 수: 9 (최근 30일)
Hanif Juma
Hanif Juma 2017년 7월 27일
댓글: Noam Greenboim 2020년 5월 21일
Hello all,
I am writing a parameter text file that is later used by some really really old C code I'm using. The code needs the parameter text file to be saved as UTF-16 (uint16) I believe. My current methodology is the following:
f = fopen(fid,'w');
fprintf(f,'REAL parameter_first = 5\n');
fclose(f);
In terminal a quick file -bi file.params text/plain; charset=us-ascii
How do I save it as a UTF-16 ?? Thank you in advance!!

채택된 답변

Jan
Jan 2017년 7월 27일
편집: Jan 2017년 7월 27일
There is no unique identifier to mark a text file as UTF16. Note that UINT16 is something different. Try this:
f = fopen(fid,'w');
fwrite(f, ['REAL parameter_first = 5', char(10)], 'uint16');
fclose(f);
This uses UINT16 as format, but this works only because Matlab uses UINT16 to store CHAR variables.
Does your import program need a "BOM"? See https://en.wikipedia.org/wiki/Byte_order_mark#UTF-16

추가 답변 (1개)

Walter Roberson
Walter Roberson 2017년 7월 27일
Guessing about the byte order:
f = fopen(fid, 'w', 'ieee-be', 'UTF-16BE');
fprintf(f, 'REAL parameter_first = 5\n');
fclose(fid)
Matters get more complicated if you need to write out characters whose code points are above 255: in such a case you need to use fwrite(), and the task of converting codepoints is most easily done with unicode2native() to produce a sequence of uint8 that you fwrite()

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by