一、Byte与String转换
string转Byte
byte[] byteArray = System.Text.Encoding.Default.GetBytes( str );
byte[]转成string:
string str = System.Text.Encoding.Default.GetString( byteArray );
其实,在System.Text.Encoding class中,还有很多其它有用的方法,像GetChars,从一个byte[]转成一个char[],等等,可以参考MSDN。
另外,还有其它编码方式的,如System.Text.UTF8Encoding class、System.Text.UnicodeEncoding class等,根据不同需要可选不同的class。
二、Byte的16进制值转字符串
[]Byte tcpPacket.PayloadData
BitConverter.ToString(tcpPacket.PayloadData);
| Type | 转换为字节 | 从字节转换 |
|---|---|---|
| Boolean | GetBytes(Boolean) | ToBoolean |
| Char | GetBytes(Char) | ToChar |
| Double | GetBytes(Double) - 或 - DoubleToInt64Bits(Double) | ToDouble - 或 - Int64BitsToDouble |
| Int16 | GetBytes(Int16) | ToInt16 |
| Int32 | GetBytes(Int32) | ToInt32 |
最后
以上就是跳跃未来最近收集整理的关于C#字符串操作的全部内容,更多相关C#字符串操作内容请搜索靠谱客的其他文章。
发表评论 取消回复