Csharp/CSharp Tutorial/I18N Internationalization/Encoding Unicode
Версия от 15:31, 26 мая 2010; (обсуждение)
Содержание
Big Endian Unicode Bytes
using System;
using System.Text;
public class MainClass
{
static void Main() {
string str = "abc!";
Encoding unicode = Encoding.Unicode;
Encoding beUnicode = Encoding.BigEndianUnicode;
byte[] unicodeBytes = unicode.GetBytes(str);
byte[] beUnicodeBytes = Encoding.Convert( unicode,
beUnicode,
unicodeBytes);
Console.WriteLine( "Big Endian Unicode Bytes:" );
StringBuilder sb = new StringBuilder();
foreach( byte b in beUnicodeBytes ) {
sb.Append( b ).Append(" : ");
}
Console.WriteLine( "{0}\n", sb.ToString() );
}
}
Big Endian Unicode Bytes: 0 : 97 : 0 : 98 : 0 : 99 : 0 : 33 :
Convert UTF8 from Unicode
using System;
using System.Text;
public class MainClass
{
static void Main() {
string str = "abc!";
Encoding unicode = Encoding.Unicode;
Encoding utf8 = Encoding.UTF8;
byte[] unicodeBytes = unicode.GetBytes(str);
byte[] utf8Bytes = Encoding.Convert( unicode,
utf8,
unicodeBytes );
Console.WriteLine( "UTF Bytes:" );
StringBuilder sb = new StringBuilder();
foreach( byte b in utf8Bytes ) {
sb.Append( b ).Append(" : ");
}
Console.WriteLine( sb.ToString() );
}
}
UTF Bytes: 97 : 98 : 99 : 33 :
Encodings: Encoding.GetEncoding(1252)
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;
using System.Globalization;
public class MainClass
{
public static void Main()
{
Encoding westernEuropean = Encoding.GetEncoding(1252);
Console.WriteLine(westernEuropean);
}
}
System.Text.SBCSCodePageEncoding
Unicode encode
using System;
using System.Text;
public class MainClass
{
static void Main() {
string str = "abc!";
Encoding unicode = Encoding.Unicode;
byte[] unicodeBytes = unicode.GetBytes(str);
Console.WriteLine( "Orig. String: {0}\n", str );
Console.WriteLine( "Little Endian Unicode Bytes:" );
StringBuilder sb = new StringBuilder();
foreach( byte b in unicodeBytes ) {
sb.Append( b ).Append(" : ");
}
Console.WriteLine( "{0}\n", sb.ToString() );
}
}
Orig. String: abc! Little Endian Unicode Bytes: 97 : 0 : 98 : 0 : 99 : 0 : 33 : 0 :