using System;
using System.Text;
class Program
{
static void Main()
{
// 定义一个字符串
string str = "Hello, World!";
// 使用 Encoding.UTF8.GetBytes 方法将字符串转换为字节数组
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 编码的字节数组。这个方法会根据 UTF-8 编码规则将每个字符转换为对应的字节值。如果你需要其他编码方式(如 ASCII 或 Unicode),可以分别使用 Encoding.ASCII.GetBytes
或 Encoding.Unicode.GetBytes
。
上一篇:c# 时间比较
下一篇:c# int 最大值
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站