String to ASCII : UTF8 UTF16 « File Stream « C# / C Sharp






String to ASCII

     
using System;

public static class StringExtension
{
    public static byte[] ToAscii(this String s)
    {
        byte[] retval = new byte[s.Length];
        for (int ix = 0; ix < s.Length; ++ix)
        {
            char ch = s[ix];
            if (ch <= 0x7f) retval[ix] = (byte)ch;
            else retval[ix] = (byte)'?';
        }
        return retval;
    }
}

   
    
    
    
    
  








Related examples in the same category

1.Convert UTF-8 and ASCII encoded bytes back to UTF-16 encoded stringConvert UTF-8 and ASCII encoded bytes back to UTF-16 encoded string
2.UTF8 decoder
3.Guess Text File Encoding
4.Byte Endian