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

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

C#
郎朗坤 2019-10-12 13:07:57
如何在C#中將結(jié)構(gòu)轉(zhuǎn)換為字節(jié)數(shù)組?如何在C#中將結(jié)構(gòu)轉(zhuǎn)換為字節(jié)數(shù)組?我定義了這樣一個結(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)建它的一個實(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ā)送這個包。為此,我需要將結(jié)構(gòu)轉(zhuǎn)換為字節(jié)數(shù)組。我該怎么做?代碼片段是什么?
查看完整描述

3 回答

?
精慕HU

TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個贊

如果您真的希望它是快速的,您可以使用不安全的代碼與CopyMemory。CopyMemory大約快5倍(例如,800 MB的數(shù)據(jù)通過編組復(fù)制需要3s,而通過CopyMemory復(fù)制只需要0.6 s)。此方法限制您只使用實(shí)際存儲在structblob本身中的數(shù)據(jù),例如數(shù)字或固定長度字節(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;
    }



查看完整回答
反對 回復(fù) 2019-10-13
?
臨摹微笑

TA貢獻(xiàn)1982條經(jīng)驗(yàn) 獲得超2個贊

看看這些方法:

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);}

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

更新*有關(guān)詳細(xì)信息,請參閱來源



查看完整回答
反對 回復(fù) 2019-10-13
  • 3 回答
  • 0 關(guān)注
  • 368 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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