字节到字符串 C#

王林
发布: 2024-09-03 15:21:41
原创
550 人浏览过

在本文中,我们将学习将字节数组转换为字符串。有很多方法可以帮助我们实现这一目标。这些方法中的一种常见方法是使用 System 命名空间中存在的 BitConverter 类。在本主题中,我们将学习 C# 字节到字符串。

BitConverter.ToString() 方法及其所有重载形式可以轻松将 byte[] 转换为字符串。此方法基本上将数字值(只不过是 byte[] 的元素)转换为其等效的十六进制形式的字符串。重载的形式如下:

  • ToString(Byte[]);
  • ToString(Byte[], Int32);
  • ToString(Byte[], Int32, Int32);

语法与解释

以下是使用 BitConverter.ToString() 方法将 byte[] 转换为字符串的语法:

public static string ToString(byte[] byteArray);
登录后复制

上面的方法接受一个字节数组作为输入,并返回一个包含一些十六进制对的字符串。每对都由连字符分隔,代表 byteArray 中的相应元素。

public static string ToString(byte[] byteArray, int startingIndex);
登录后复制

这里,ToString() 方法有两个参数; byteArray 是要转换为字符串的字节数组,startingIndex 是要开始转换的字节数组中元素的索引。

public static string ToString(byte[] byteArray, int startingIndex, int length);
登录后复制

这里,ToString() 方法接受三个参数; byteArray 是要转换为字符串的字节数组,startingIndex 是要执行转换的字节数组中元素的索引,length 是从startingIndex 开始要转换的字节数组元素的数量。

如何在 C# 中将字节转换为字符串?

如前所述,在 C# 中将字节数组转换为字符串的方法有很多种。常见的方法之一是使用 BitConverter.ToString() 方法。 C# 中 System 命名空间下的 BitConverter 类包含多个将字节数组转换为基本数据类型的方法,因此我们可以使用该类的 ToString() 方法将 byte[] 转换为字符串。该方法有以下三种重载形式:

ToString(byte[]);
登录后复制

此方法用于将整个字节数组的每个元素的数值转换为字符串,其中结果字符串将包含十六进制对,每个对由连字符分隔,每对代表相应的字节数组元素。

ToString(byte[], Int32);
登录后复制

此方法将字节子数组中每个元素的数值转换为其等效的十六进制字符串对。此方法中的整数参数指定子数组的起始索引。

ToString(byte[], Int32, Int32);
登录后复制

此方法将字节数组中部分或全部元素的数值转换为其十六进制字符串对。使用此方法的第二个和第三个参数指定要转换的元素;第二个参数指定我们需要开始转换的起始索引,第三个参数指定要获取的元素的长度,即要转换的元素数量,从前面指定的起始索引开始。

除此之外,我们还可以使用 System.out.println() 中存在的 Encoding 类。文本命名空间,用于将字节数组转换为具有 UTF-8 或 ASCII 字符集和编码的字符串。该类提供的 GetString() 方法用于将字节数组中存在的字节解码为字符串。

  • UTF8.GetString(byte[]);
  • ASCII.GetString(byte[]);

Encoding类提供的其他编码方案包括Unicode、UTF32、UTF7等

实现此转换的另一种方法是使用 Convert.ToBase64String() 方法,用于将字节数组中存在的字节转换为字符串。

ToBase64String(byte[]); We can also use MemoryStream to convert byte array to string. But, first, we need to convert the byte array to the stream of bytes using MemoryStream class; then, we can read this entire stream using StreamReader class and then can return this stream as a string with the help of the ReadToEnd() method. Let us now understand this with the help of statements provided below:
登录后复制
using (MemoryStream memoryStream = new MemoryStream(bytes))
{
using (StreamReader streamReader = new StreamReader(memoryStream))
{
return streamReader.ReadToEnd();
}
}
登录后复制

上述语句中的“bytes”是要转换为字符串的字节数组。因此,首先,我们获取字节数组作为“memoryStream”对象中的字节流。然后,我们使用 StreamReader 类读取该流,并使用 ReadToEnd() 方法将流作为字符串返回,该方法读取流并返回字符串值。

C# 字节到字符串的示例

下面提到了不同的示例:

示例#1

使用 BitConverter 类将字节数组转换为字符串的示例。

代码:

using System;
using System.Globalization;
using System.Text;
using System.IO;
public class Program
{
public static void Main()
{
string resultedStr = string.Empty;
//defining byte array
byte[] bytes = new byte[5] { 12, 24, 36, 48, 60 };
//printing byte array before conversion
Console.Write("Byte array before conversion: ");
for (int i = 0; i < bytes.Length; i++)
{
Console.Write(" " + bytes[i]);
}
//converting byte array to string
resultedStr = BitConverter.ToString(bytes);
//printing string after conversion
Console.WriteLine("\nResulted string after conversion: {0}",
resultedStr);
Console.ReadLine();
}
}
登录后复制

输出:

字节到字符串 C#

示例#2

使用 Encoding 类和 MemoryStream 类将字节数组转换为字符串的示例。

代码:

using System;
using System.Globalization;
using System.Text;
using System.IO;
namespace ConsoleApp4
{
public class Program
{
public static void Main()
{
string str = "The sun rises in the east";
//converting string to array of bytes
byte[] bytes = Encoding.ASCII.GetBytes(str);
//printing byte array before conversion
Console.Write("Byte array before conversion: ");
for (int i = 0; i < bytes.Length; i++)
{
Console.Write(" " + bytes[i]);
}
//converting byte array to string using Encoding class
str = Encoding.ASCII.GetString(bytes);
//printing resulted string after conversion
Console.WriteLine("\n");
Console.Write("\nConversion using Encoding class: \n{0}",
str);
//converting byte array to string using MemoryStream class
Console.WriteLine("\n");
Console.WriteLine("\nConversion using MemoryStream: ");
using (MemoryStream memoryStream = new MemoryStream(bytes))
{
using (StreamReader streamReader = new StreamReader(memoryStream))
{
Console.Write(streamReader.ReadToEnd());
}
}
Console.ReadLine();
}
}
}
登录后复制

输出:

字节到字符串 C#

结论

在 C# 中,我们可以使用 BitConverter、Encoding、MemoryStream 等类将字节数组转换为字符串。BitConverter 类提供的结果字符串包括十六进制对。使用 Encoding 类,我们可以使用相同的编码方案将字符串转换为 byte[] 以及将 byte[] 转换为字符串。

以上是字节到字符串 C#的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!