将char []转换为byte [],反之亦然
在C#2010中将char[]
转换为byte[]
的最佳方法是什么?
请注意,我们事先不知道单个char
的格式.它可以是ASCII或Unicode或UTF-8或UTF-16.
是否有一种通用的转换方法可以克服这一问题?
在这种情况下,对Unicode来说反之亦然吗?
What is the best way to convert char[]
to byte[]
in C# 2010?
Please note that we do not know in advance the format of a single char
. It can either be ASCII or Unicode or UTF-8 or UTF-16.
Is there one universal conversion to overcome this?
And what is the vice versa conversion in this case, say to Unicode?
还有更多解决方案.
第一个是:
There are more solutions.
The first is:
byte[] byteArray = Encoding.yourEncoding.GetBytes(charArray1);
有关此方法的更多信息:
http://msdn.microsoft.com/en-us/library/5881c0k9.aspx [ ^ ]
第二个是:
More information about this method:
http://msdn.microsoft.com/en-us/library/5881c0k9.aspx[^]
The second is:
byte[] byteArray = Convert.FromBase64CharArray(charArray1,0,charArray1.Length);
有关此方法的更多信息: http://msdn.microsoft.com/zh-cn/library/system.convert.frombase64chararray.aspx [ ^ ]
希望对您有所帮助.
More information about this method: http://msdn.microsoft.com/en-us/library/system.convert.frombase64chararray.aspx[^]
Hope this helps.
查看此处: ^ ]
原来我根本不需要它.我试图通过一个字符串来序列化char [],我可以简单地调用它:
序列化:
-使用String(char [])构造函数将char []转换为String;
反序列化:
-调用String.ToCharArray();
Turns out I didn''t need it after all. I was trying to serialize char[] via a string, for which I could simply call:
Serialization:
- Convert char[] to String using String(char[]) constructor;
De-serialization:
- Call String.ToCharArray();