C#_string_byte数组转换解析
![star](https://csdnimg.cn/release/downloadcmsfe/public/img/star.98a08eaa.png)
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 array)之间的转换是常见的操作,尤其在处理二进制数据、网络传输或文件读写时。本篇文章将详细解析C#中如何进行这两种数据类型的转换,并讨论转换过程中需要注意的事项。 我们来看最基础的转换方法: 1. **字符串(string)转为字节数组(byte[])**: 这个过程涉及到字符编码,C#中可以使用`System.Text.Encoding`类的不同实例来完成。默认情况下,`System.Text.Encoding.Default`通常代表系统的ANSI编码。例如: ```csharp string str = "example"; byte[] byteArray = System.Text.Encoding.Default.GetBytes(str); ``` 如果需要其他编码,比如UTF-8,可以使用`System.Text.UTF8Encoding`: ```csharp byte[] byteArrayUtf8 = System.Text.UTF8Encoding.UTF8.GetBytes(str); ``` 2. **字节数组(byte[])转回字符串(string)**: 反向操作同样简单,只需使用`GetString()`方法: ```csharp string strFromBytes = System.Text.Encoding.Default.GetString(byteArray); string strFromBytesUtf8 = System.Text.UTF8Encoding.UTF8.GetString(byteArrayUtf8); ``` 在某些特定场景下,我们需要转换为16进制表示的字符串。例如,一个字节`0xAE00CF`需要转换为字符串"ae00cf"。这里提供了一个将字节数组转换为16进制字符串的辅助函数: ```csharp public static string ToHexString(byte[] bytes) { string hexString = string.Empty; if (bytes != null) { StringBuilder strB = new StringBuilder(); for (int i = 0; i < bytes.Length; i++) { strB.Append(bytes[i].ToString("X2")); // "X2"表示16进制且两位宽 } hexString = strB.ToString(); } return hexString; } ``` 此函数会将每个字节转换为两个字符的16进制字符串,并连接起来。 相反,如果有一个16进制表示的字符串,比如"ae00cf",需要转换回字节数组,我们可以用以下方法: ```csharp public static byte[] GetBytes(string hexString, out int discarded) { discarded = 0; string newString = ""; char c; // remove all none A-F, 0-9, characters for (int i = 0; i < hexString.Length; i++) { c = hexString[i]; if (IsHexDigit(c)) newString += c; else discarded++; } // if odd number of characters, discard last character if (newString.Length % 2 != 0) { discarded++; newString = newString.Substring(0, newString.Length - 1); } int byteLength = newString.Length / 2; byte[] bytes = new byte[byteLength]; string hex; int j = 0; for (int i = 0; i < newString.Length; i += 2) { hex = new String(new Char[] { newString[j], newString[j + 1] }); bytes[i / 2] = HexToByte(hex); j = j + 2; } return bytes; } private static byte HexToByte(string hex) { return Convert.ToByte(hex, 16); } ``` 这个函数会去除非16进制字符,并将剩余的16进制字符串转换为字节数组。 在进行这些转换时,需要注意以下几点: - 字符编码的选择至关重要,因为不同的编码方式会导致不同的字节序列。例如,UTF-8和Unicode编码对非ASCII字符的表示方式不同。 - 当处理16进制字符串时,确保字符串的长度是偶数,因为一个字节由两个16进制字符表示。 - 对于16进制字符串到字节数组的转换,可能需要检查和处理非法字符。 - 检查字符串中的空格或其他非数字字符,它们可能会影响转换结果。 理解和掌握C#中字符串与字节数组之间的转换对于处理数据是非常重要的。了解各种编码方式以及16进制转换可以帮助你在处理二进制数据时更加游刃有余。在实际应用中,一定要注意选择正确的编码方式,以避免数据丢失或错误。
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![text/x-c++](https://img-home.csdnimg.cn/images/20210720083646.png)
![cs](https://img-home.csdnimg.cn/images/20210720083646.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![cs](https://img-home.csdnimg.cn/images/20210720083646.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/release/download_crawler_static/6746133/bg1.jpg)
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
- 威武堂堂2021-12-28。。。给新手看的,以为有高论的可以跑了
- huwei991682014-03-04内容不是很全。
- 「已注销」2014-02-22C#r入门者不错的资源。
![avatar](https://profile-avatar.csdnimg.cn/201652235a8142f18eed74def2833731_shangsongwww.jpg!1)
- 粉丝: 4w+
- 资源: 135
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)