将char []转换为byte [],反之亦然 [英] Convert char[] to byte[] and vice versa

查看:99
本文介绍了将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();


这篇关于将char []转换为byte [],反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆