zoukankan      html  css  js  c++  java
  • C# 字符串与字节数组相互转换

        总结归纳:如果直接从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; }
    System.Text.Encoding

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

     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);
    System.Text.Encoding.GetBytes(String str)

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

     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);
    System.Text.Encoding.GetString(byte[] byte)

    三、 字符串转换成流

    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));
    System.IO.MemoryStream

    四、流转换成字符串

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

    五、字节数组转换成流

    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);
    流转换成字节数组
  • 相关阅读:
    基于风险集成 Risk-Based
    基于消息(事件)集成 Message-Based/Event-Based
    如何保证系统测试的完备性?
    1.1 功能测试(功能)
    问:给你一个网站,你如何测试?
    问:你在测试中发现了一个bug,但是开发经理认为这不是一个bug,你应该怎样解决?
    4.如何重新加载 Spring Boot 上的更改,而无需重新启动服务器?
    什么是 JavaConfig?
    Spring Boot 有哪些优点?
    .Redis中的常用命令哪些?
  • 原文地址:https://www.cnblogs.com/xiaoqingshe/p/5882601.html
Copyright © 2011-2022 走看看