public struct abc //
{
// public byte zhenlb_0;
// public byte zhenchd_1;
public byte xinxiid_2;
public int ads;
}
private void test_Click_1(object sender, EventArgs e)
{
abc ab=new abc();
//ab.zhenlb_0=0x11;
//ab.zhenchd_1=0x22;
ab.xinxiid_2=0x33;
ab.ads = 0x11223344; byte[] a = fun.StructureToByteArrayEndian(ab);
}代码大概是如上, 我的测试代码。
结构体里面 有一个int 和byte,但是 public byte[] StructureToByteArrayEndian(object obj)
{
object thisBoxed = obj; //copy ,将 struct 装箱
Type test = thisBoxed.GetType();
int offset = 0;
int size = Marshal.SizeOf(obj);size中获取 结构体的大小是8. 应该是5啊, 这样创建的数组就比实际结构体中的数要多,数组中多出来很多0. 求助,
菜鸟.给跪了..
{
// public byte zhenlb_0;
// public byte zhenchd_1;
public byte xinxiid_2;
public int ads;
}
private void test_Click_1(object sender, EventArgs e)
{
abc ab=new abc();
//ab.zhenlb_0=0x11;
//ab.zhenchd_1=0x22;
ab.xinxiid_2=0x33;
ab.ads = 0x11223344; byte[] a = fun.StructureToByteArrayEndian(ab);
}代码大概是如上, 我的测试代码。
结构体里面 有一个int 和byte,但是 public byte[] StructureToByteArrayEndian(object obj)
{
object thisBoxed = obj; //copy ,将 struct 装箱
Type test = thisBoxed.GetType();
int offset = 0;
int size = Marshal.SizeOf(obj);size中获取 结构体的大小是8. 应该是5啊, 这样创建的数组就比实际结构体中的数要多,数组中多出来很多0. 求助,
菜鸟.给跪了..
解决方案 »
- 你们身边有架构师么?什么是架构师?
- C# 的using System.Runtime.Serialization.Json;
- 如何将数据添加到自定义的DataTable中?
- 我装了C#,现在要打开一个VC++.net的工程,还需要重新装VC++.net的开发环境吗?
- c#中怎样操作INI文件?
- VC中的动态数组,C#可实现吗?
- c#如何使用打印功能把和数据库绑定的一堆textBox控件中的内容打印出来?
- 请问各位高手自己用C#开发的Windows组件可以加到网页中吗?
- 控件引用问题——知道的帮帮忙吧~~
- c#中怎样获取系统的一些信息?比如系统路径,“我的文档”的路径?
- 求个能制作酷狗魔方界面的思路和相应需要的知识点(本人在校学生)
- C# 在控件上画的直线丢失
{
public byte xinxiid_2;
public int ads; public Int32 Size
{
get { return 5; }
}
}abc的长度又不会变化
直接写死就行了
public struct abc
{
public byte xinxiid_2;
public int ads;
}这是另外的
public struct abc
{
public byte xinxiid_2;
public int ads;
public String ss; public Int32 Size
{
get { return 5 + System.Text.Encoding.UTF8.GetByteCount(ss); }
}
}
public struct abc
{
[FieldOffset(0)]
public byte xinxiid_2;
[FieldOffset(1)]
public int ads;
}
这样应该可以了,不过注意端序的问题。
解决办法
1.如果是为了在数组和结构体间转换的话,调用StructureToPtr和PtrToStructure就好,想要得到byte,可以把制定为强制转换为byte
2.自己利用方法来将定义的结构体转换成数组,这个可以把结构体改成类,添加个计算个变量的byte总和的私有方法或静态方法都可以。
纯属脱裤子放屁
多此一举
如果struct里只有基本类型
在定义struct时就已经知道长度了
不需要在外面再SizeOf计算一次如果struct里含有引用类型,在外面用几次SizeOf都是不正确的,如:
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct abc
{
public byte xinxiid_2;
public int ads;
public String ss;
public DataSet ds;
public DataTable dt;
}
using System.Runtime.InteropServices;
class Program
{
[StructLayout(LayoutKind.Explicit, Size = 5)]
public struct abc
{
[FieldOffset(0)]
public byte xinxiid_2;
[FieldOffset(1)]
public int ads;
}
[StructLayout(LayoutKind.Sequential, Size = 5)]
public struct _bytes
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)]
public byte[] data;
}
static void Main(string[] args)
{
abc x = new abc();
x.xinxiid_2 = 17;
x.ads = 0x00ffccaa;
Console.WriteLine(Marshal.SizeOf(x));
IntPtr p = System.Runtime.InteropServices.Marshal.AllocHGlobal(5);
System.Runtime.InteropServices.Marshal.StructureToPtr(x, p, false);
_bytes d = (_bytes)System.Runtime.InteropServices.Marshal.PtrToStructure(p, typeof(_bytes));
Array.ForEach(d.data, b => Console.WriteLine(b.ToString("X")));
Console.ReadLine();
}
}然后把abc的特性StructLayout构造参数改为LayoutKind.Sequential、去掉Size = 5,把_bytes中data长度改为8看看有什么不同。