Determine a string's encoding in C#

前端 未结 9 2030
小鲜肉
小鲜肉 2020-11-22 14:54

Is there any way to determine a string\'s encoding in C#?

Say, I have a filename string, but I don\'t know if it is encoded in Unicode UTF-16 or the

9条回答
  •  长发绾君心
    2020-11-22 15:48

    Another option, very late in coming, sorry:

    http://www.architectshack.com/TextFileEncodingDetector.ashx

    This small C#-only class uses BOMS if present, tries to auto-detect possible unicode encodings otherwise, and falls back if none of the Unicode encodings is possible or likely.

    It sounds like UTF8Checker referenced above does something similar, but I think this is slightly broader in scope - instead of just UTF8, it also checks for other possible Unicode encodings (UTF-16 LE or BE) that might be missing a BOM.

    Hope this helps someone!

提交回复
热议问题