MemoryStream from string - confusion about Encoding to use

Assuming applicationForm is a string you read from some UTF8 text file. It will be UTF16/Unicode, whatever the encoding of the source file. The conversion happened when you loaded the file into the string.

Your code will encode the applicationForm string into a MemoryStream of UTF8 bytes.

This may or may not be correct depending on what you want to do with it.

.Net strings are always UTF16 or Unicode. When Strings are converted to files, streams or byte[], they can be encoded in different ways. 1 byte is not enough to store all the different characters used in all languages so more complicated strings need to be encoded so one character can be represented by more than one byte, Sometimes or always depending on the encoding used.

If you use a simple encoding like ASCII one character will always comprise of one byte but the data will be limited to the ASCII character set. Converting to 'ASCII' from any UTF encoding could lose data if any multi-byte characters are used.

For the complete picture on unicode go here.

EDIT 1: Barring further info on the GenerateApplicationForm component, enconding UTF8 is likely to be the right choice. If that doesn't work, try ASCII or UTF16. Best of all, consult the component source code or the component provider.

EDIT 2: Definitely UTF8 then, you were right all along.

Tags:

C#

.Net

Encoding