Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to check the encoding of a C# string? [duplicate]

Possible Duplicate:
Determine a string's encoding in C#

I believe if I create a string it defaults to UTF8, however if the string is created else where and I want to be extra safe before dealing with it and check what its encoding is I do not see any easy way to do that using the string or Encoding class. Am I missing something or is a C# string always UTF8 no matter what?

like image 888
Rodney S. Foley Avatar asked Aug 10 '11 17:08

Rodney S. Foley


1 Answers

Strings in C# (well, .NET) don't have encoding, effectively... or you can view them all as UTF-16, given that they're a sequence of char values, which are UTF-16 code units.

Normally, however, you only need to care about encoding when you convert from a string to a binary form (e.g. down a socket or to a file). At that point, you should specify the encoding explicitly - the string itself has no concept of this.

The only aspect which "defaults" to UTF-8 is that there are plenty of .NET APIs which are overloaded to either accept an encoding or not, and if no encoding is specified, UTF-8 is used. File.ReadAllText is an example of this. However, after reading the file there's no distinction between "text which was read from a UTF-8 file" and "text which was read from a Big5 file" etc.

like image 103
Jon Skeet Avatar answered Dec 14 '22 20:12

Jon Skeet