读取二进制问题,字节数组到结构体转换 本帖最后由 winbq5 于 2010-11-02 19:56:22 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 ,[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]中的CharSet = CharSet.Auto经实验,CharSet的各变量对char以及char[]的影响如下:ANSI:char以及char[]占一个字节AUTO:char以及char[]占两个字节UNICODE:char以及char[]占两个字节 MSDN 对于 CharSet 有说明:128 个元素的 char 数组的大小为 256 字节。在固定大小的 char 缓冲区中,每个字符始终占用两个字节,而与编码无关。即使将 char 缓冲区封送到具有 CharSet = CharSet.Auto 或 CharSet = CharSet.Ansi 的 API 方法或结构,也是如此。// 单精度浮点型 sizeof(float) = 4// 你需要的 Marshal.SizeOf(typeof(RadarSite)) 是多少?// 我这个输出是 48// 你试试看 不知道能不能用[StructLayout(LayoutKind.Sequential)]public struct RadarSite{ [MarshalAs(UnmanagedType.LPStr, SizeConst = 30)] public string country; [MarshalAs(UnmanagedType.LPStr, SizeConst = 20)] public string province; [MarshalAs(UnmanagedType.LPStr, SizeConst = 40)] public string station; [MarshalAs(UnmanagedType.LPStr, SizeConst = 10)] public string stationNumber; [MarshalAs(UnmanagedType.LPStr, SizeConst = 20)] public string radarType; [MarshalAs(UnmanagedType.LPStr, SizeConst = 16)] public string longitude; [MarshalAs(UnmanagedType.LPStr, SizeConst = 16)] public string latitude; public float longitudeValue; public float latitudeValue; public float height; public short maxAngle; public short optiAngle; public short mangFreq;} C#中bindingNavigator上的按钮怎么变灰了 传值问题 如何在RTF控件中显示RTF文档内容? treeView展开问题? 请教实现数据追加更新的好方法! 注册表问题!!!在线等 帮帮我啊!!!!!! 我写的办公自动化(OA)软件(含扫描插件) 简单问题,如何在form里面画一个填充的长方形? 怎样设置控件的left和top坐标是其容器的觉得位置? C#中如何关闭某个进程的网络连接 怎么用正则匹配关键字?
ANSI:char以及char[]占一个字节
AUTO:char以及char[]占两个字节
UNICODE:char以及char[]占两个字节
128 个元素的 char 数组的大小为 256 字节。在固定大小的 char 缓冲区中,每个字符始终占用两个字节,而与编码无关。即使将 char 缓冲区封送到具有 CharSet = CharSet.Auto 或 CharSet = CharSet.Ansi 的 API 方法或结构,也是如此。
// 单精度浮点型 sizeof(float) = 4// 你需要的 Marshal.SizeOf(typeof(RadarSite)) 是多少?
// 我这个输出是 48
// 你试试看 不知道能不能用[StructLayout(LayoutKind.Sequential)]
public struct RadarSite
{
[MarshalAs(UnmanagedType.LPStr, SizeConst = 30)]
public string country; [MarshalAs(UnmanagedType.LPStr, SizeConst = 20)]
public string province; [MarshalAs(UnmanagedType.LPStr, SizeConst = 40)]
public string station; [MarshalAs(UnmanagedType.LPStr, SizeConst = 10)]
public string stationNumber; [MarshalAs(UnmanagedType.LPStr, SizeConst = 20)]
public string radarType; [MarshalAs(UnmanagedType.LPStr, SizeConst = 16)]
public string longitude; [MarshalAs(UnmanagedType.LPStr, SizeConst = 16)]
public string latitude; public float longitudeValue;
public float latitudeValue;
public float height;
public short maxAngle;
public short optiAngle;
public short mangFreq;
}