gpt4 book ai didi

c# - C++ 到 C# 数组声明

转载 作者:太空宇宙 更新时间:2023-11-03 18:17:58 24 4
gpt4 key购买 nike

我想将以下代码转换为 C#:

struct Elf32_Ehdr {
uint8 e_ident[16]; // Magic number and other info
uint16 e_type; // Object file type
uint16 e_machine; // Architecture
uint32 e_version; // Object file version
uint32 e_entry; // Entry point virtual address
uint32 e_phoff; // Program header table file offset
uint32 e_shoff; // Section header table file offset
uint32 e_flags; // Processor-specific flags
uint16 e_ehsize; // ELF header size in bytes
uint16 e_phentsize; // Program header table entry size
uint16 e_phnum; // Program header table entry count
uint16 e_shentsize; // Section header table entry size
uint16 e_shnum; // Section header table entry count
uint16 e_shstrndx; // Section header string table index
};

显然,它映射到不同的大小写。uint16 -> UInt16
uint32 -> UInt32
uint64 -> UInt64

显然,uint8 映射到 Byte。

问题是:

Byte   e_ident[16];   // Magic number and other info<br />


不会编译,它说: Array size cannot be declared in variable declaration...

什么,没有新的就没有固定大小的数组?

将其映射到此是否正确:

Byte[] e_ident = new Byte[16];   // Magic number and other info



还是结果会完全错误?

最佳答案

您将需要 Structlayout和 MarshalAs 属性,并像这样使用它:

//untested
[Structlayout]
struct Elf32_Ehdr
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=16)]
Byte e_ident[16]; // Magic number and other info
Uint16 e_type; // Object file type
...
}

但请将此视为进一步搜索的提示,这不是我知道的很多。

关于c# - C++ 到 C# 数组声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3487028/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com