using System;
using System.Text;
class Program
{
static void Main()
{
// 定义一个字符串
string originalString = "Hello, World!";
// 使用 UTF8 编码将字符串转换为字节数组
byte[] byteArray = Encoding.UTF8.GetBytes(originalString);
// 输出转换后的字节数组
Console.WriteLine("Byte array:");
foreach (byte b in byteArray)
{
Console.Write(b + " ");
}
}
}
originalString,其内容为 "Hello, World!"。Encoding.UTF8.GetBytes 方法将字符串转换为字节数组。这里选择了 UTF-8 编码方式,你也可以根据需要选择其他编码(如 ASCII、Unicode 等)。希望这段代码和解释对你有帮助!
上一篇:c#foreach用法
下一篇:c# get set用法
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站