可以使用 Encoding 类的 GetBytes 方法将字符串转换为字节数组。

示例代码:

string str = 'Hello World!';
byte[] bytes = Encoding.UTF8.GetBytes(str);

在此示例中,我们将字符串 'Hello World!' 转换为 UTF8 编码的字节数组。

注意:在将字符串转换为字节数组时,需要指定字符串的编码格式。常用的编码格式包括 UTF8、UTF16、ASCII 等。如果不指定编码格式,则默认使用系统当前的编码格式。

C# 字符串转换为字节数组 (byte[]) - 使用 Encoding.GetBytes() 方法

原文地址: https://www.cveoy.top/t/topic/nPFf 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录