http://msdn.microsoft.com/zh-cn/library/system.text.asciiencoding.aspx 下面的示例演示了如何将 Unicode 字符编码为 ASCII 字符。 请注意,当应用程序使用 ASCIIEncoding 对 ASCII 范围外的 Unicode 字符进行编码时,会发生数据丢失。自行修改为从ASCII字符编码为 Unicode 字符即可 using System; using System.Text;class ASCIIEncodingExample { public static void Main() { // The encoding. ASCIIEncoding ascii = new ASCIIEncoding(); // A Unicode string with two characters outside the ASCII code range. String unicodeString = "This Unicode string contains two characters " + "with codes outside the ASCII code range, " + "Pi (\u03a0) and Sigma (\u03a3)."; Console.WriteLine("Original string:"); Console.WriteLine(unicodeString); // Save positions of the special characters for later reference. int indexOfPi = unicodeString.IndexOf('\u03a0'); int indexOfSigma = unicodeString.IndexOf('\u03a3'); // Encode string. Byte[] encodedBytes = ascii.GetBytes(unicodeString); Console.WriteLine(); Console.WriteLine("Encoded bytes:"); foreach (Byte b in encodedBytes) { Console.Write("[{0}]", b); } Console.WriteLine(); // Notice that the special characters have been replaced with // the value 63, which is the ASCII character code for '?'. Console.WriteLine(); Console.WriteLine( "Value at position of Pi character: {0}", encodedBytes[indexOfPi] ); Console.WriteLine( "Value at position of Sigma character: {0}", encodedBytes[indexOfSigma] ); // Decode bytes back to string. // Notice missing Pi and Sigma characters. String decodedString = ascii.GetString(encodedBytes); Console.WriteLine(); Console.WriteLine("Decoded bytes:"); Console.WriteLine(decodedString); } }
下面的示例演示了如何将 Unicode 字符编码为 ASCII 字符。 请注意,当应用程序使用 ASCIIEncoding 对 ASCII 范围外的 Unicode 字符进行编码时,会发生数据丢失。自行修改为从ASCII字符编码为 Unicode 字符即可
using System;
using System.Text;class ASCIIEncodingExample {
public static void Main() {
// The encoding.
ASCIIEncoding ascii = new ASCIIEncoding(); // A Unicode string with two characters outside the ASCII code range.
String unicodeString =
"This Unicode string contains two characters " +
"with codes outside the ASCII code range, " +
"Pi (\u03a0) and Sigma (\u03a3).";
Console.WriteLine("Original string:");
Console.WriteLine(unicodeString); // Save positions of the special characters for later reference.
int indexOfPi = unicodeString.IndexOf('\u03a0');
int indexOfSigma = unicodeString.IndexOf('\u03a3'); // Encode string.
Byte[] encodedBytes = ascii.GetBytes(unicodeString);
Console.WriteLine();
Console.WriteLine("Encoded bytes:");
foreach (Byte b in encodedBytes) {
Console.Write("[{0}]", b);
}
Console.WriteLine(); // Notice that the special characters have been replaced with
// the value 63, which is the ASCII character code for '?'.
Console.WriteLine();
Console.WriteLine(
"Value at position of Pi character: {0}",
encodedBytes[indexOfPi]
);
Console.WriteLine(
"Value at position of Sigma character: {0}",
encodedBytes[indexOfSigma]
); // Decode bytes back to string.
// Notice missing Pi and Sigma characters.
String decodedString = ascii.GetString(encodedBytes);
Console.WriteLine();
Console.WriteLine("Decoded bytes:");
Console.WriteLine(decodedString);
}
}
你告诉我,怎么把常用的5000个汉字,编到ASCII中