使用 System.Text.Encoding.Unicode.GetBytes(aString) 时是否应该检查字节顺序?
Should I check endianness when using System.Text.Encoding.Unicode.GetBytes(aString)?
我正在编写将数据转换为字节 [] 的东西,通过互联网传输,然后转换回它们用于 Unity 游戏项目的内容。
我用BitConverter
转换int
,float
等,如下例所示:
float aFloat = 312321f;
var bytes = BitConverter.GetBytes(aFloat);
if (BitConverter.IsLittleEndian) Array.Reverse(bytes);
// sending through the internet
byte[] bytes = GetByteArrayFromTheInternet();
if (BitConverter.IsLittleEndian) Array.Reverse(bytes);
float aFloat = BitConverter.ToSingle(bytes, 0);
我在发送数据之前和之后进行字节序检查以确保它们相同。我需要为 string
执行此操作吗?
string aString = "testing";
var bytes = System.Text.Encoding.Unicode.GetBytes(aString);
// if (BitConverter.IsLittleEndian) Array.Reverse(bytes); // Do I need this line?
// sending through the internet
byte[] bytes = GetByteArrayFromTheInternet();
// if (BitConverter.IsLittleEndian) Array.Reverse(bytes); // Do I need this too?
string aString = System.Text.Encoding.Unicode.GetString(bytes);
提前致谢!
I do the endianess check before and after sending the data to make sure they're the same. Do I need to do this for string?
这取决于您在网络上与谁通话。他们使用什么字节顺序?
在您的第一个示例中,您假设网络协议总是 发送float
类型(32 位浮点)作为大端。哪个好;传统上,“网络主机顺序”一直是big-endian,因此它是网络协议的不错选择。
但是没有要求网络协议必须遵守该协议,也没有要求它在内部是自洽的,而且您还没有提供有关您使用的协议的任何信息实施。
注意:“网络协议”指的是应用层协议。这将类似于 HTTP、SMTP、FTP、POP 等。即无论您的应用程序为网络上的字节格式选择什么。
因此,您必须查阅所用协议的规范以了解 Unicode 编码 (UTF16) 数据使用的字节顺序。我 猜测 它是大端,因为你的 float
值也是。但我不能肯定地说。
请注意,如果网络协议确实将文本编码为大端 UTF16,则您无需自己为每个字符交换字节。只需使用 BigEndianUnicode
编码对象对文本进行编码和解码。它将为您处理字节顺序。
另请注意,使用正确的编码器并不是真正可选的。检查 BitConverter.IsLittleEndian
字段告诉您的是当前的 CPU 体系结构。但是如果网络协议上的文本被编码为big-endian,那么即使你是运行 on a big-endian CPU,你仍然需要使用BigEndianUnicode
编码。就像总是可靠地解码大端文本一样,Unicode
编码 总是 解码文本就好像它是小端一样,即使 运行 在大端 CPU.
我正在编写将数据转换为字节 [] 的东西,通过互联网传输,然后转换回它们用于 Unity 游戏项目的内容。
我用BitConverter
转换int
,float
等,如下例所示:
float aFloat = 312321f;
var bytes = BitConverter.GetBytes(aFloat);
if (BitConverter.IsLittleEndian) Array.Reverse(bytes);
// sending through the internet
byte[] bytes = GetByteArrayFromTheInternet();
if (BitConverter.IsLittleEndian) Array.Reverse(bytes);
float aFloat = BitConverter.ToSingle(bytes, 0);
我在发送数据之前和之后进行字节序检查以确保它们相同。我需要为 string
执行此操作吗?
string aString = "testing";
var bytes = System.Text.Encoding.Unicode.GetBytes(aString);
// if (BitConverter.IsLittleEndian) Array.Reverse(bytes); // Do I need this line?
// sending through the internet
byte[] bytes = GetByteArrayFromTheInternet();
// if (BitConverter.IsLittleEndian) Array.Reverse(bytes); // Do I need this too?
string aString = System.Text.Encoding.Unicode.GetString(bytes);
提前致谢!
I do the endianess check before and after sending the data to make sure they're the same. Do I need to do this for string?
这取决于您在网络上与谁通话。他们使用什么字节顺序?
在您的第一个示例中,您假设网络协议总是 发送float
类型(32 位浮点)作为大端。哪个好;传统上,“网络主机顺序”一直是big-endian,因此它是网络协议的不错选择。
但是没有要求网络协议必须遵守该协议,也没有要求它在内部是自洽的,而且您还没有提供有关您使用的协议的任何信息实施。
注意:“网络协议”指的是应用层协议。这将类似于 HTTP、SMTP、FTP、POP 等。即无论您的应用程序为网络上的字节格式选择什么。
因此,您必须查阅所用协议的规范以了解 Unicode 编码 (UTF16) 数据使用的字节顺序。我 猜测 它是大端,因为你的 float
值也是。但我不能肯定地说。
请注意,如果网络协议确实将文本编码为大端 UTF16,则您无需自己为每个字符交换字节。只需使用 BigEndianUnicode
编码对象对文本进行编码和解码。它将为您处理字节顺序。
另请注意,使用正确的编码器并不是真正可选的。检查 BitConverter.IsLittleEndian
字段告诉您的是当前的 CPU 体系结构。但是如果网络协议上的文本被编码为big-endian,那么即使你是运行 on a big-endian CPU,你仍然需要使用BigEndianUnicode
编码。就像总是可靠地解码大端文本一样,Unicode
编码 总是 解码文本就好像它是小端一样,即使 运行 在大端 CPU.