什么 .NET UnmanagedType 是 Unicode (UTF-16)?
我将字节打包到一个结构中,其中一些对应于 Unicode 字符串。以下内容对于 ASCII 字符串效果很好:
[StructLayout(LayoutKind.Sequential)]
private struct PacketBytes
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public string MyString;
}
我假设我可以
[StructLayout(LayoutKind.Sequential)]
private struct PacketBytes
{
[MarshalAs(UnmanagedType.LPWStr, SizeConst = 32)]
public string MyString;
}
将其设为 Unicode,但这不起作用(字段值为空,其他字段的值不正确,表明字节解包被搞乱了)。 (由于该字段是具有其他字段的结构的一部分,为了清楚起见,我省略了这些字段,因此我不能简单地更改包含结构的 CharSet。)
知道我做错了什么吗?
这是输入(64 字节,小端):
31:00:31:00:32:00:33:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
输出应该是 Unicode 字符串“1123”。
I am packing bytes into a struct, and some of them correspond to a Unicode string. The following works fine for an ASCII string:
[StructLayout(LayoutKind.Sequential)]
private struct PacketBytes
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public string MyString;
}
I assumed that I could do
[StructLayout(LayoutKind.Sequential)]
private struct PacketBytes
{
[MarshalAs(UnmanagedType.LPWStr, SizeConst = 32)]
public string MyString;
}
to make it Unicode, but that didn't work (the field value was empty and the other fields had incorrect values, indicating that the byte unpacking was messed up). (Since this field is part of a struct with other fields, which I've omitted for clarity, I can't simply change the CharSet of the containing struct.)
Any idea what I'm doing wrong?
Here is the input (64 bytes, little endian):
31:00:31:00:32:00:33:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
The output should be the Unicode string "1123".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将通过声明字符串类型的嵌套结构来做到这一点。 “内部”结构可以声明其
CharSet
。这类似于我博客上的解决方案: http:// nitoprograms.blogspot.com/2010/02/interop-multiDimension-arrays-of.html例如:
I would do this by declaring a nested structure for the string type. The "inner" structure can declare its
CharSet
. This is similar to the solution on my blog: http://nitoprograms.blogspot.com/2010/02/interop-multidimensional-arrays-of.htmle.g.:
感谢@StephenCleary。我的完整解决方案是这样的:
Credit to @StephenCleary. My full solution is this: