RSS Feed for This PostCurrent Article

.NET: Detect String Encoding

Here is a simple way to detect if your content is ASCII or Unicode

public void CheckForEncoding(string content)
       {
           int i = 0;
           for (i = 1; i <= content.Length; i++)
           {
               int code = Convert.ToInt32(Convert.ToChar(content.Substring(i - 1, 1))) ;
               if (code < 0 || code > 255)
               {
                   Console.WriteLine("Unicode");
                   return;
               }
           }
           Console.WriteLine("ascii");
       }

Popularity: 2% [?]


Trackback URL


RSS Feed for This PostPost a Comment