可以使用 System.Text.Encoding 类中的 GetBytes 方法将字符串转换为 byte[] 数据。例如:

string str = 'Hello, world!';
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(str);

其中,GetBytes 方法的参数指定了编码方式,这里使用了 ASCII 编码。如果需要使用其他编码方式,可以在 GetBytes 方法中传递对应的 Encoding 对象。

例如,使用 UTF-8 编码:

byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str);
C# 字符串转字节数组 (byte[]) 详解

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

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