onkami
onkami

Reputation: 9431

Convert input file or stream to UTF8 and detect if it's UTF8 or not (asp.net)

what would be the best way in C#/asp.net to do the following: - detect if file is reliably UTF8 or not; - convert input stream or just file on disk to UTF8 programmatically.

Cheers Askar

Upvotes: 2

Views: 8676

Answers (1)

sara
sara

Reputation: 3934

For checking if stream is UTF8 see: http://utf8checker.codeplex.com/

For converting it you can use StreamReader:

new StreamReader(stream, Encoding.UTF8)) 

In fact, using this contructor prevents you the previous check, you can just use it.

MSDN:

The character encoding is set by the encoding parameter, and the buffer size is set to 1024 bytes. The StreamReader object attempts to detect the encoding by looking at the first three bytes of the stream. It automatically recognizes UTF-8, little-endian Unicode, and big-endian Unicode text if the file starts with the appropriate byte order marks. Otherwise, the user-provided encoding is used. See the Encoding.GetPreamble method for more information.

If you wish it not to detect little-endian Unicode and big-endian Unicode call the following contructor:

new StreamReader(stream, Encoding.UTF8, false)) 

The false will turn off the Encoding detection, and thus this call will allways encode the file using UTF 8.

Upvotes: 6

Related Questions