Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to convert a structure to a byte array in C#?
    primarykey
    data
    text
    <p>How do I convert a structure to a byte array in C#?</p> <p>I have defined a structure like this:</p> <pre><code>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; } </code></pre> <p>In my main method, I create an instance of it and assign values to it:</p> <pre><code>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"; </code></pre> <p>Now I want to send this Packet by socket. For that, I need to convert the structure to a byte array. How can I do it?</p> <p>My full code is as follows.</p> <pre><code>static void Main(string[] args) { Socket MyPing = new Socket(AddressFamily.InterNetwork, SocketType.Stream , ProtocolType.Unspecified ) ; MyPing.Connect("172.24.18.240", 139); //Fake an IP Address so I can send with SendTo IPAddress IP = new IPAddress(new byte[] { 172,24,18,240 }); IPEndPoint IPEP = new IPEndPoint(IP, 139); //Local IP for Receiving IPEndPoint Local = new IPEndPoint(IPAddress.Any, 0); EndPoint EP = (EndPoint)Local; 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"; MyPing.SendTo(It takes byte array as parameter); } </code></pre> <p>What would a code snippet be?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload