Forum > General

Writing Chinese Characters on text

(1/2) > >>

crazydog:
How can I write Chinese Characters on a .txt file?
when I use the rewrite function,
the Chinese Characters become gibberish on the .txt

eg
中學部(in Program) >>>>> 銝剖飛??(In Text)

engkin:
Show the code you used.

What did you use to see or open the txt file, Notepad?

What OS/Lazarus/FPC version?

Meanwhile the following code gives the attached text file:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program chtest;{$mode objfpc}{$H+} uses  Classes; var  sl: TStringList; begin  sl := TStringList.Create;  try    sl.Add('中學部');    sl.SaveToFile('ch-test.txt');  finally    sl.Free;  end;end.

crazydog:

--- Quote from: engkin on August 05, 2017, 06:02:42 am ---Show the code you used.

What did you use to see or open the txt file, Notepad?

What OS/Lazarus/FPC version?

Meanwhile the following code gives the attached text file:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program chtest;{$mode objfpc}{$H+} uses  Classes; var  sl: TStringList; begin  sl := TStringList.Create;  try    sl.Add('中學部');    sl.SaveToFile('ch-test.txt');  finally    sl.Free;  end;end.
--- End quote ---
I'm using Lazarus IDE v1.4.4
Here's my Code,
It seems that it worked for txt, (didnt realize it b4)
but when it is .csv and opened by Microsoft Office Excel,
'中學部' turns into '銝剖飛??'

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure AddData;var Data:TextFile;begin  assign(Data,'Data.csv');  rewrite(Data);  write(Data,'中學部');  Closefile(Data)end;          

Zath:
Is it changing it to a different version of Chinese ?
Simplified and traditional are often available but maybe not to all applications on your system.

engkin:

--- Quote from: crazydog on August 06, 2017, 08:24:06 am ---I'm using Lazarus IDE v1.4.4
Here's my Code,
It seems that it worked for txt, (didnt realize it b4)
but when it is .csv and opened by Microsoft Office Excel,
'中學部' turns into '銝剖飛??'

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure AddData;var Data:TextFile;begin  assign(Data,'Data.csv');  rewrite(Data);  write(Data,'中學部');  Closefile(Data)end;          
--- End quote ---

That's an Excel bug (or feature). It considers the csv file encoding to have the same encoding as your system (which is not true, your system has an ansi encoding, while the file is UTF8).

A workaround is to use UTF16LE encoding with BOM marker.

Edit:
Simple test:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses  LazUTF8,...;...procedure AddData;var Data:File;  s: String;  ss: WideString;  { or UnicodeString }begin  assign(Data,'Data.csv');  rewrite(Data);  Reset(Data, 1);  BlockWrite(Data, #$FF#$FE, 2);  { Write BOM marker }  s := '中學部';  { UTF8 }  ss := s; { or ss := UTF8ToUTF16(s); } { UTF16 }  BlockWrite(Data, ss[1], Length(RawByteString(ss)));  Closefile(Data)end;

Navigation

[0] Message Index

[#] Next page

Go to full version