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

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

如何在C#中將結(jié)構(gòu)轉(zhuǎn)換為字節(jié)數(shù)組?

如何在C#中將結(jié)構(gòu)轉(zhuǎn)換為字節(jié)數(shù)組?

HUX布斯 2019-07-17 14:35:52
如何在C#中將結(jié)構(gòu)轉(zhuǎn)換為字節(jié)數(shù)組?如何在C#中將結(jié)構(gòu)轉(zhuǎn)換為字節(jié)數(shù)組?我定義了這樣一個(gè)結(jié)構(gòu):public struct CIFSPacket{     public uint protocolIdentifier; //The value must be "0xFF+'SMB'".     public byte command;     public byte errorClass;     public byte reserved;     public ushort error;     public byte flags;     //Here there are 14 bytes of data which is used differently among different dialects.     //I do want the flags2. However, so I'll try parsing them.     public ushort flags2;     public ushort treeId;     public ushort processId;     public ushort userId;     public ushort multiplexId;     //Trans request     public byte wordCount;//Count of parameter words defining the data portion of the packet.     //From here it might be undefined...     public int parametersStartIndex;     public ushort byteCount; //Buffer length     public int bufferStartIndex;     public string Buffer;}在我的主要方法中,我創(chuàng)建它的一個(gè)實(shí)例并給它賦值:CIFSPacket packet = new CIFSPacket();packet.protocolIdentifier = 0xff;packet.command = (byte)CommandTypes.SMB_COM_NEGOTIATE; packet.errorClass = 0xff;packet.error = 0;packet.flags = 0x00;packet.flags2 = 0x0001;packet.multiplexId = 22;packet.wordCount = 0; packet.byteCount = 119;packet.Buffer = "NT LM 0.12";現(xiàn)在我想用套接字發(fā)送這個(gè)包。為此,我需要將結(jié)構(gòu)轉(zhuǎn)換為字節(jié)數(shù)組。我該怎么做?代碼片段是什么?
查看完整描述

3 回答

?
冉冉說(shuō)

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

如果您真的希望它是快速的,您可以使用不安全的代碼與CopyMemory。CopyMemory大約快5倍(例如,800 MB的數(shù)據(jù)通過(guò)編組復(fù)制需要3s,而通過(guò)CopyMemory復(fù)制只需要0.6 s)。此方法限制您只使用實(shí)際存儲(chǔ)在structblob本身中的數(shù)據(jù),例如數(shù)字或固定長(zhǎng)度字節(jié)數(shù)組。

    [DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)]
    private static unsafe extern void CopyMemory(void *dest, void *src, int count);

    private static unsafe byte[] Serialize(TestStruct[] index)
    {
        var buffer = new byte[Marshal.SizeOf(typeof(TestStruct)) * index.Length];
        fixed (void* d = &buffer[0])
        {
            fixed (void* s = &index[0])
            {
                CopyMemory(d, s, buffer.Length);
            }
        }

        return buffer;
    }


查看完整回答
反對(duì) 回復(fù) 2019-07-17
?
瀟湘沐

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

看看這些方法:

byte [] StructureToByteArray(object obj){
    int len = Marshal.SizeOf(obj);

    byte [] arr = new byte[len];

    IntPtr ptr = Marshal.AllocHGlobal(len);

    Marshal.StructureToPtr(obj, ptr, true);

    Marshal.Copy(ptr, arr, 0, len);

    Marshal.FreeHGlobal(ptr);

    return arr;}void ByteArrayToStructure(byte [] bytearray, ref object obj){
    int len = Marshal.SizeOf(obj);

    IntPtr i = Marshal.AllocHGlobal(len);

    Marshal.Copy(bytearray,0, i,len);

    obj = Marshal.PtrToStructure(i, obj.GetType());

    Marshal.FreeHGlobal(i);}

這是一個(gè)無(wú)恥的副本,另一個(gè)線(xiàn)程,我發(fā)現(xiàn)谷歌!

更新*有關(guān)詳細(xì)信息,請(qǐng)參閱來(lái)源


查看完整回答
反對(duì) 回復(fù) 2019-07-17
  • 3 回答
  • 0 關(guān)注
  • 369 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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