在 C# 中,可以使用 `System.Text.Encoding.UTF8.GetBytes(key)` 来实现与 Java 中 `StringUtils.getBytesUtf8(key)` 相同的功能。

示例代码如下:

using System;
using System.Text;

public class Program
{
    public static void Main(string[] args)
    {
        string key = "Hello World";
        byte[] bytes = Encoding.UTF8.GetBytes(key);
        
        Console.WriteLine(BitConverter.ToString(bytes)); // 输出十六进制表示的字节数组
        
        // 其他操作...
    }
}

输出结果为:

48-65-6C-6C-6F-20-57-6F-72-6C-64

注意:C# 中的 `byte` 类型是有符号的,而 Java 中的 `byte` 类型是无符号的。所以在 C# 中打印字节数组时,会以有符号的十六进制表示。若需要以无符号的十六进制表示,可以使用 `bytes[i].ToString("X2")` 来实现。


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

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