在C#中将字节读入结构

在C#中将字节读入结构,第1张

概述时间换另一个问题.我正在为我的小型3D引擎项目编写MD2加载程序.在我的旧语言(C)中,我可以定义一个结构,然后从打开的文件中直接读取()到结构中.我有一个结构来保存MD2文件中的标题信息,如下所示: [StructLayout(LayoutKind.Sequential)]public struct MD2_Header{ public int FourCC; public 时间换另一个问题.我正在为我的小型3D引擎项目编写MD2加载程序.在我的旧语言(C)中,我可以定义一个结构,然后从打开的文件中直接读取()到结构中.我有一个结构来保存MD2文件中的标题信息,如下所示:
[StructLayout(LayoutKind.Sequential)]public struct MD2_header{    public int FourCC;    public int Version;    public int TextureWIDth;    public int TextureHeight;    public int FrameSizeInBytes;    public int NbrTextures;    public int NbrVertices;    public int NbrTextureCoords;    public int NbrTriangles;    public int NbropenGLCmds;    public int NbrFrames;    public int TextureOffset;    public int TexCoordOffset;    public int TriangleOffset;    public int FrameOffset;    public int OpenGLCmdOffset;    public int EndOffset;}

在我的读者代码中,我想做的事情如下:

// Suck the MD2 header into a structure,it is 68 bytes long.Classic.Util.MD2_header md2hdr = new Classic.Util.MD2_header();md2hdr = reader.ReadBytes(sizeof(Classic.Util.MD2_header));

我意识到这是不正确的,因为它有点奇怪地打破了类型安全,但你知道我想要完成什么.我可以通过单独调用reader.ReadInt32()来做到这一点,但我很好奇是否有任何方式让我按照我希望使用普通库调用的方式工作.

我看了一下Marshal.copy()方法,但似乎是在托管和非托管内存之间进行,这不是我在这里做的.

有什么建议?

解决方法 C中的结构和C#中的结构是两个完全不同的东西. C中的结构既用于值类型又用于引用类型,而C#中的结构仅用于值类型.

值类型应该表示单个值,但是您拥有的是大量值,因此您应该使用类. .NET中结构的建议最大大小为16个字节,您的数据量是其四倍以上.

具有属性的类和采用字节数组的构造函数如下所示:

public class MD2_header {  public int FourCC { get; set; }  public int Version { get; set; };  public int TextureWIDth { get; set; };  public int TextureHeight { get; set; };  public int FrameSizeInBytes { get; set; };  public int NbrTextures { get; set; };  public int NbrVertices { get; set; };  public int NbrTextureCoords { get; set; };  public int NbrTriangles { get; set; };  public int NbropenGLCmds { get; set; };  public int NbrFrames { get; set; };  public int TextureOffset { get; set; };  public int TexCoordOffset { get; set; };  public int TriangleOffset { get; set; };  public int FrameOffset { get; set; };  public int OpenGLCmdOffset { get; set; };  public int EndOffset { get; set; };  public MD2_header(byte[] values) {    FourCC = BitConverter.ToInt32(values,0);    Version = BitConverter.ToInt32(values,4);    TextureWIDth = BitConverter.ToInt32(values,8);    TextureHeight = BitConverter.ToInt32(values,12);    FrameSizeInBytes = BitConverter.ToInt32(values,16);    NbrTextures = BitConverter.ToInt32(values,20);    NbrVertices = BitConverter.ToInt32(values,24);    NbrTextureCoords = BitConverter.ToInt32(values,28);    NbrTriangels = BitConverter.ToInt32(values,32);    NbropenGLCmds = BitConverter.ToInt32(values,36);    NbrFrames = BitConverter.ToInt32(values,40);    TextureOffset = BitConverter.ToInt32(values,44);    TexCoordOffset = BitConverter.ToInt32(values,48);    TriangleOffset = BitConverter.ToInt32(values,52);    FrameOffset = BitConverter.ToInt32(values,56);    OpenGLCmdOffset = BitConverter.ToInt32(values,60);    EndOffset = BitConverter.ToInt32(values,64);  }}
总结

以上是内存溢出为你收集整理的在C#中将字节读入结构全部内容,希望文章能够帮你解决在C#中将字节读入结构所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1240825.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存