登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

面包会有的

... ...

 
 
 

日志

 
 

C# 字符串与字节数组相互转换  

2017-02-10 22:39:55|  分类: C# |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
总结归纳:如果直接从System.String类中找到方法进行字符串和字节数组之间的转换,是不太可能的。为了使其之间进行转换,需要借助另外一个类型:System.Text.Encoding。这个类型提供了将C#字符串转换成字节数组的方法,也提供了将C# 字节数组转换成字符串。

System.Text.Encoding类型的默认构造函数不太可用,不过该类型提供了几种默认的静态属性。如下:

1 //
 2         // 摘要:
 3         //     获取 ASCII(7 位)字符集的编码。
 4         //
 5         // 返回结果:
 6         //     ASCII(7 位)字符集的编码。
 7         public static Encoding ASCII { get; }
 8         //
 9         // 摘要:
10         //     获取使用 Big Endian 字节顺序的 UTF-16 格式的编码。
11         //
12         // 返回结果:
13         //     获取使用 Big Endian 字节顺序的 UTF-16 格式的编码对象。
14         public static Encoding BigEndianUnicode { get; }
15         //
16         // 摘要:
17         //     获取操作系统的当前 ANSI 代码页的编码。
18         //
19         // 返回结果:
20         //     操作系统的当前 ANSI 代码页的编码。
21         public static Encoding Default { get; }
22         //
23         // 摘要:
24         //     获取使用 Little-Endian 字节顺序的 UTF-16 格式的编码。
25         //
26         // 返回结果:
27         //     使用 Little-Endian 字节顺序的 UTF-16 格式的编码。
28         public static Encoding Unicode { get; }
29         //
30         // 摘要:
31         //     获取使用 Little-Endian 字节顺序的 UTF-32 格式的编码。
32         //
33         // 返回结果:
34         //     使用 Little-Endian 字节顺序的 UTF-32 格式的编码对象。
35         public static Encoding UTF32 { get; }
36         //
37         // 摘要:
38         //     获取 UTF-7 格式的编码。
39         //
40         // 返回结果:
41         //     UTF-7 格式的编码。
42         public static Encoding UTF7 { get; }
43         //
44         // 摘要:
45         //     获取 UTF-8 格式的编码。
46         //
47         // 返回结果:
48         //     UTF-8 格式的编码。
49         public static Encoding UTF8 { get; }

一、字符串转换成字节数组

1 //System.Text.Encoding.GetBytes(String str)
 2             //1. 字符串转换字节数组
 3 
 4             string str1 = "character string1";
 5             var strToBytes1 = System.Text.Encoding.UTF8.GetBytes(str1);
 6 
 7             string str2 = "character string2";
 8             var strToBytes2 = System.Text.Encoding.Default.GetBytes(str2);
 9 
10             string str3 = "character string3";
11             var strToBytes3 = System.Text.Encoding.Unicode.GetBytes(str3);
12 
13             string str4 = "character string4";
14             var strToBytes4 = System.Text.Encoding.ASCII.GetBytes(str4);
15 
16             string str5 = "character string5";
17             var strToBytes5 = System.Text.Encoding.UTF32.GetBytes(str5);
18 
19             string str6 = "character string6";
20             var strToBytes6 = System.Text.Encoding.UTF7.GetBytes(str6);

二、字节数组转换成字符串

1 //2. 字节数组转换成字符串
 2 
 3             Console.Write("strToBytes1 使用UTF8编码转换成字符串:");
 4             var byteToString1 = System.Text.Encoding.UTF8.GetString(strToBytes1);
 5             Console.WriteLine(byteToString1);
 6             Console.Write("strToBytes1 使用Default编码转换成字符串:");
 7             var byteToString2 = System.Text.Encoding.Default.GetString(strToBytes2);
 8             Console.WriteLine(byteToString2);
 9             Console.Write("strToBytes1 使用Unicode编码转换成字符串:");
10             var byteToString3 = System.Text.Encoding.Unicode.GetString(strToBytes3);
11             Console.WriteLine(byteToString3);
12             Console.Write("strToBytes1 使用ASCII编码转换成字符串:");
13             var byteToString4 = System.Text.Encoding.ASCII.GetString(strToBytes4);
14             Console.WriteLine(byteToString4);
15             Console.Write("strToBytes1 使用UTF32编码转换成字符串:");
16             var byteToString5 = System.Text.Encoding.UTF32.GetString(strToBytes5);
17             Console.WriteLine(byteToString5);
18             Console.Write("strToBytes1 使用UTF7编码转换成字符串:");
19             var byteToString6 = System.Text.Encoding.UTF7.GetString(strToBytes6);
20             Console.WriteLine(byteToString6);

三、 字符串转换成流

1   //System.IO.MemoryStream
2             //3. 字符串转换成流
3             System.IO.MemoryStream ms1 = new System.IO.MemoryStream(strToBytes2);
4             System.IO.MemoryStream ms2 = new System.IO.MemoryStream(Convert.FromBase64String(str1));

四、流转换成字符串

1   //System.IO.MemoryStream
2             //4. 流转换成字符串
3             var memoryToString1 = System.Text.Encoding.Default.GetString(ms2.ToArray());
4             var memoryToString2 = Convert.ToBase64String(ms2.ToArray());

五、字节数组转换成流

1   //5. 字节数组转换成流
2             System.IO.MemoryStream ms3 = new System.IO.MemoryStream(strToBytes1);
3 
4             System.IO.MemoryStream ms4 = new System.IO.MemoryStream(); ms4.Read(strToBytes1, 0, strToBytes1.Length);

六、流转换成字节数组

1 //6. 流转换成字节数组
2 
3 byte[] bt = ms3.ToArray();
4 
5 System.IO.MemoryStream ms = new System.IO.MemoryStream();
6 ms.Write(bt, 0, (int)ms4.Length);
  评论这张
 
阅读(462)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018