你如何将字符串转换为.NET中的字节数组?数组、转换为、字符串、字节

2023-09-02 10:35:20 作者:一鹿都晗着暖阳

我有我需要转换为.NET字节数组相当于一个字符串。

I have a string that I need to convert to the equivalent array of bytes in .NET.

这应该是容易的,但我有脑抽筋。

This ought to be easy, but I am having a brain cramp.

推荐答案

您需要使用的编码告诉.NET你所期望的输出。例如,使用UTF-16:

You need to use an encoding to tell .NET what you expect as the output. For example, in UTF-16:

var result = System.Text.Encoding.Unicode.GetBytes(text);