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


what best way in c#/asp.net following: - detect if file reliably utf8 or not; - convert input stream or file on disk utf8 programmatically.

cheers askar

for checking if stream utf8 see: http://utf8checker.codeplex.com/

for converting can use streamreader:

new streamreader(stream, encoding.utf8))  

in fact, using contructor prevents previous check, can use it.

msdn:

the character encoding set encoding parameter, , buffer size set 1024 bytes. streamreader object attempts detect encoding looking @ first 3 bytes of stream. automatically recognizes utf-8, little-endian unicode, , big-endian unicode text if file starts appropriate byte order marks. otherwise, user-provided encoding used. see encoding.getpreamble method more information.

if wish not detect little-endian unicode , big-endian unicode call following contructor:

new streamreader(stream, encoding.utf8, false))  

the false turn off encoding detection, , call allways encode file using utf 8.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -