第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何將C ++結(jié)構(gòu)轉(zhuǎn)換為等效的C#?

如何將C ++結(jié)構(gòu)轉(zhuǎn)換為等效的C#?

C#
溫溫醬 2021-05-19 16:14:41
這是我的C ++結(jié)構(gòu)(文檔說每個(gè)實(shí)例的大小必須恰好是10個(gè)字節(jié)):#pragma pack (1)struct LaserPoint {    WORD x;    WORD y;    byte colors[6];};我制作了以下C#結(jié)構(gòu):[StructLayout(LayoutKind.Sequential, Pack=1)]public struct LaserPoint {    public UInt16 x;                   // 2 bytes    public UInt16 y;                   // 2 bytes    public byte[] colors;              // 6 bytes}這是我的C#項(xiàng)目中的完整測試代碼:using System;using System.Runtime.InteropServices;namespace StructSizeTest {    class Program {        [StructLayout(LayoutKind.Sequential, Pack=1)]        public struct LaserPoint {            public UInt16 x;                   // 2 bytes            public UInt16 y;                   // 2 bytes            public byte[] colors;              // byte[6] = 6 bytes        }        static void Main(string[] args) {            LaserPoint point = new LaserPoint();            point.x = (UInt16)16384;            point.y = (UInt16)32768;            point.colors = new byte[6];            point.colors[0] = 255;            point.colors[1] = 255;            point.colors[2] = 255;            point.colors[3] = 255;            point.colors[4] = 255;            point.colors[5] = 255;            Console.WriteLine("LaserPoint.Size: " + Marshal.SizeOf(point));            Console.ReadLine();        }    }}這是控制臺(tái)上的輸出:LaserPoint.Size: 8為什么是point8個(gè)字節(jié)而不是10個(gè)字節(jié)?UInt16  = 2 bytesUInt16  = 2 bytesbyte[6] = 6 bytesTotal   = 10 bytes ?我在這里想念的是什么?
查看完整描述

2 回答

?
慕妹3242003

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超6個(gè)贊

我認(rèn)為問題出在數(shù)組上...試試這個(gè):


[StructLayout(LayoutKind.Sequential, Pack=1)]

public struct LaserPoint {

    public UInt16 x;                   // 2 bytes

    public UInt16 y;                   // 2 bytes

    [MarhalAs(UnmanagedType.ByValArray, SizeConst = 6]

    public byte[] colors;              // 6 bytes

}


查看完整回答
反對(duì) 回復(fù) 2021-05-23
  • 2 回答
  • 0 關(guān)注
  • 183 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)