string s = "aa"; byte[] array = Encoding.GetEncoding("ASCII").GetBytes(s);array 就是,把ASCII换成UTF-8和BigEndian
参考了MSDN的示例:static void Main(string[] args) { string unicodeString = "汉字测试字符串"; // Create two different encodings. Encoding be = Encoding.BigEndianUnicode; Encoding unicode = Encoding.Unicode; // Convert the string into a byte[]. byte[] unicodeBytes = unicode.GetBytes(unicodeString); // Perform the conversion from one encoding to the other. byte[] beBytes = Encoding.Convert(unicode, be, unicodeBytes);
// Convert the new byte[] into a char[] and then into a string. // This is a slightly different approach to converting to illustrate // the use of GetCharCount/GetChars. char[] beChars = new char[be.GetCharCount(beBytes, 0, beBytes.Length)]; be.GetChars(beBytes, 0, beBytes.Length, beChars, 0); string beString = new string(beChars); // Display the strings created before and after the conversion. Console.WriteLine("Original string: {0}", unicodeString); Console.WriteLine("Ascii converted string: {0}", beString);}
将字符转为Unicode数字public string ConvertString(string value, int fromBase, int toBase) { int intValue = Convert.ToInt32(value, fromBase); return Convert.ToString(intValue, toBase); }
using System; using System.IO; using System.Globalization; using System.Text;public class Encoding_UnicodeToCP { public static void Main() { // Converts ASCII characters to bytes. // Displays the string's byte representation in the // specified code page. // Code page 1252 represents Latin characters. PrintCPBytes("Hello, World!",1252); // Code page 932 represents Japanese characters. PrintCPBytes("Hello, World!",932); // Converts Japanese characters to bytes. PrintCPBytes("\u307b,\u308b,\u305a,\u3042,\u306d",1252); PrintCPBytes("\u307b,\u308b,\u305a,\u3042,\u306d",932); } public static void PrintCPBytes(string str, int codePage) { Encoding targetEncoding; byte[] encodedChars; // Gets the encoding for the specified code page. targetEncoding = Encoding.GetEncoding(codePage); // Gets the byte representation of the specified string. encodedChars = targetEncoding.GetBytes(str); // Prints the bytes. Console.WriteLine ("Byte representation of '{0}' in Code Page '{1}':", str, codePage); for (int i = 0; i < encodedChars.Length; i++) Console.WriteLine("Byte {0}: {1}", i, encodedChars[i]); } }
byte[] array = Encoding.GetEncoding("ASCII").GetBytes(s);array 就是,把ASCII换成UTF-8和BigEndian
{
string unicodeString = "汉字测试字符串"; // Create two different encodings.
Encoding be = Encoding.BigEndianUnicode;
Encoding unicode = Encoding.Unicode; // Convert the string into a byte[].
byte[] unicodeBytes = unicode.GetBytes(unicodeString); // Perform the conversion from one encoding to the other.
byte[] beBytes = Encoding.Convert(unicode, be, unicodeBytes);
// Convert the new byte[] into a char[] and then into a string.
// This is a slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.
char[] beChars = new char[be.GetCharCount(beBytes, 0, beBytes.Length)];
be.GetChars(beBytes, 0, beBytes.Length, beChars, 0);
string beString = new string(beChars); // Display the strings created before and after the conversion.
Console.WriteLine("Original string: {0}", unicodeString);
Console.WriteLine("Ascii converted string: {0}", beString);}
{
int intValue = Convert.ToInt32(value, fromBase);
return Convert.ToString(intValue, toBase);
}
using System.IO;
using System.Globalization;
using System.Text;public class Encoding_UnicodeToCP
{
public static void Main()
{
// Converts ASCII characters to bytes.
// Displays the string's byte representation in the
// specified code page.
// Code page 1252 represents Latin characters.
PrintCPBytes("Hello, World!",1252);
// Code page 932 represents Japanese characters.
PrintCPBytes("Hello, World!",932); // Converts Japanese characters to bytes.
PrintCPBytes("\u307b,\u308b,\u305a,\u3042,\u306d",1252);
PrintCPBytes("\u307b,\u308b,\u305a,\u3042,\u306d",932);
} public static void PrintCPBytes(string str, int codePage)
{
Encoding targetEncoding;
byte[] encodedChars; // Gets the encoding for the specified code page.
targetEncoding = Encoding.GetEncoding(codePage); // Gets the byte representation of the specified string.
encodedChars = targetEncoding.GetBytes(str); // Prints the bytes.
Console.WriteLine
("Byte representation of '{0}' in Code Page '{1}':", str,
codePage);
for (int i = 0; i < encodedChars.Length; i++)
Console.WriteLine("Byte {0}: {1}", i, encodedChars[i]);
}
}