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 编码方式,其他编码方式如 ASCII、Unicode 等也可以根据需要选择。如果你需要使用其他编码方式,可以替换 Encoding.UTF8 为其他编码类,例如 Encoding.ASCII 或 Encoding.Unicode。
上一篇:c#时间戳转换日期格式输出
下一篇:c# @
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站