没有合适的资源?快使用搜索试试~ 我知道了~
C#_string_byte数组转换解析

温馨提示
C# string byte数组转换解析 C# string byte数组转换实现的过程是什么呢?C# string byte数组间的转换需要注意什么呢?C# string byte数组间转换所涉及的方法是什么呢?让我们来看看具体的内容: C# string byte数组转换之string类型转成byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str ); 反过来,byte[]转成string: string str = System.Text.Encoding.Default.GetString ( byteArray ); 其它编码方式的,如System.Text.UTF8Encoding,System.Text.UnicodeEncoding class等;例如: string类型转成ASCII byte[]:("01" 转成 byte[] = new byte[]{ 0x30, 0x31}) 1 byte[] byteArray = System.Text.Encoding.ASCII.GetBytes ( str ); ASCII byte[] 转成string:(byte[] = new byte[]{ 0x30, 0x31} 转成 "01") 2 string str = System.Text.Encoding.ASCII.GetString ( byteArray ); 有时候还有这样一些需求: byte[] 转成原16进制格式的string,例如0xae00cf, 转换成 "ae00cf";new byte[]{ 0x30, 0x31}转成"3031":
资源推荐
资源详情
资源评论















C# string byte 数组转换解析
C# string byte 数组转换实现的过程是什么呢?C# string byte 数组间的转换需要注意什
么呢?C# string byte 数组间转换所涉及的方法是什么呢?让我们来看看具体的内容:
C# string byte 数组转换之 string 类型转成 byte[]:
byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str );
反过来,byte[]转成 string:
string str = System.Text.Encoding.Default.GetString ( byteArray );
其它编码方式的,如 System.Text.UTF8Encoding,System.Text.UnicodeEncoding class 等;
例如:
string 类型转成 ASCII byte[]:("01" 转成 byte[] = new byte[]{ 0x30, 0x31})
1 byte[] byteArray = System.Text.Encoding.ASCII.GetBytes ( str );
ASCII byte[] 转成 string:(byte[] = new byte[]{ 0x30, 0x31} 转成 "01")
2 string str = System.Text.Encoding.ASCII.GetString ( byteArray );
有时候还有这样一些需求:
byte[] 转成原 16 进制格式的 string,例如 0xae00cf, 转换成 "ae00cf";new byte[]{ 0x30,
0x31}转成"3031":
3 public static string ToHexString ( byte[] bytes ) // 0xae00cf =>
"AE00CF "
4 {
5 string hexString = string.Empty;
6 if ( bytes != null )
7 {
8 StringBuilder strB = new StringBuilder ();
9
10 for ( int i = 0; i < bytes.Length; i++ )
11 {
12 strB.Append ( bytes[i].ToString ( "X2" ) );
13 }
14 hexString = strB.ToString ();
15 }
16 return hexString;
17 }
C# string byte 数组转换之 16 进制格式的 string 转成 byte[]
例如, "ae00cf"转换成 0xae00cf,长度缩减一半;"3031" 转成 new byte[]{ 0x30, 0x31}:
18 public static byte[] GetBytes(string hexString, out int discarded)
19 {
20 discarded = 0;
21 string newString = "";
22 char c;
23 // remove all none A-F, 0-9, characters
for (int i=0; i< SPAN>
资源评论

- 威武堂堂2021-12-28。。。给新手看的,以为有高论的可以跑了
- huwei991682014-03-04内容不是很全。
- 「已注销」2014-02-22C#r入门者不错的资源。

跨链技术践行者
- 粉丝: 4w+
- 资源: 137
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


会员权益专享
安全验证
文档复制为VIP权益,开通VIP直接复制
