Oh nooo
Oh nooo

Reputation: 488

Convert UTF16 to Windows 1250

I have a file, which I open with FileStream and I modify the text. The standard encoding of this is UTF16. After this, I would like to upload the modified file somewhere, but that has to be in a Windows-1250 format.

How is it possible to convert UTF16 to Windows-1250 encoding?

Upvotes: 1

Views: 1079

Answers (1)

David Heffernan
David Heffernan

Reputation: 613441

Use TEncoding to encode your string in a specific encoding.

var
  Ansi1250Enc: TEncoding;
....
Ansi1250Enc := TEncoding.GetEncoding(1250);

Then you can use GetBytes to encode a string as a byte array:

var
  EncodedBytes: TBytes;
  str: string;
....
str := ...; // get your text into a string variable by whatever means
EncodedBytes := Ansi1250Enc.GetBytes(str);

Or if you have the text stored in a string list you can save it like so:

Strings.SaveToFile(FileName, Ansi1250Enc);

Don't forget to destroy the Ansi1250Enc when you are done with it.

Upvotes: 4

Related Questions