using System;
using System.Text;
class Program
{
static void Main()
{
// 定义一个字符串
string str = "Hello, World!";
// 使用 UTF-8 编码将字符串转换为字节数组
byte[] bytes = Encoding.UTF8.GetBytes(str);
// 输出字节数组的内容
Console.WriteLine("Byte array contents:");
foreach (byte b in bytes)
{
Console.Write(b + " ");
}
}
}
str
,其值为 "Hello, World!"
。Encoding.UTF8.GetBytes
方法将字符串转换为字节数组。这里使用的是 UTF-8 编码,可以根据需要选择其他编码方式(如 ASCII、UTF-16 等)。如果你需要使用其他编码方式,可以替换 Encoding.UTF8
为其他编码类,例如 Encoding.ASCII
或 Encoding.Unicode
。
上一篇:c#创建数组
下一篇:c# radiobutton
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站