哪个编码是C#等效于这个java代码? “Charles Okwuagwu”.getBytes();

时间:2015-01-01 16:26:40

标签: java c# string

说我在Java中有这个代码:"Charles Okwuagwu".getBytes();

在c#中,这只是Encoding.UTF8.GetBytes("Charles Okwuagwu");

我的问题是: 1)Java使用UTF16作为字符串 2)sting内容基本上只是ASCII

仅仅使用Encoding.ASCII.GetBytes("Charles Okwuagwu");

在c#中是否等效

修改 我在.net中运行了这个小测试:

Console.WriteLine("Default:{0}", B2H(Text.Encoding.Default.GetBytes("Charles Okwuagwu")))
Console.WriteLine("ASCII:{0}", B2H(Text.Encoding.ASCII.GetBytes("Charles Okwuagwu")))
Console.WriteLine("BigEndianUnicode:{0}", B2H(Text.Encoding.BigEndianUnicode.GetBytes("Charles Okwuagwu")))
Console.WriteLine("Unicode:{0}", B2H(Text.Encoding.Unicode.GetBytes("Charles Okwuagwu")))
Console.WriteLine("UTF32:{0}", B2H(Text.Encoding.UTF32.GetBytes("Charles Okwuagwu")))
Console.WriteLine("UTF7:{0}", B2H(Text.Encoding.UTF7.GetBytes("Charles Okwuagwu")))
Console.WriteLine("UTF8:{0}", B2H(Text.Encoding.UTF8.GetBytes("Charles Okwuagwu")))

结果:

Default:436861726C6573204F6B777561677775
ASCII:436861726C6573204F6B777561677775
BigEndianUnicode:0043006800610072006C006500730020004F006B007700750061006700770075
Unicode:43006800610072006C006500730020004F006B00770075006100670077007500
UTF32:430000006800000061000000720000006C0000006500000073000000200000004F0000006B000000770000007500000061000000670000007700000075000000
UTF7:436861726C6573204F6B777561677775
UTF8:436861726C6573204F6B777561677775
似乎UTF8,UTF7,ASCII给出相同的字节。但Java字符串默认为UTF16 ...

2 个答案:

答案 0 :(得分:2)

Java中的

String.getBytes使用平台的默认编码。 所以C#的等价物是:

Encoding.Default.GetBytes("Charles Okwuagwu");

答案 1 :(得分:0)

由于Java使用UTF16,因此等效的.Net代码将是:

Encoding.Unicode.GetBytes("Charles Okwuagwu")

请参阅:http://msdn.microsoft.com/en-us/library/system.text.encoding.unicode(v=vs.110).aspx