19 Temmuz 2010, PAZARTESİ
C bir bayt dizisine bir yapıya dönüştürmek için nasıl*?
Nasıl C bir bayt dizisi için bir yapı dönüştürebilirim#?
Bu şekilde tanımlanan bir yapı var:
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;
}
Benim ana yöntem, bir örneğini oluşturmak ve bu değerler atar:
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";
Şimdi soket ile bu Paketi göndermek istiyorum. Bunun için, bir bayt dizisi için yapıya dönüştürmek istiyorum. Bunu nasıl yapabilirim?
Tam benim kod aşağıdaki gibidir.
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);
}
Kod parçacığı ne olurdu?
CEVAP
19 Temmuz 2010, PAZARTESİ
Bu oldukça kolay, sıralanırken kullanıyor.
Dosya üst
using System.Runtime.InteropServices
İşlevi
byte[] getBytes(CIFSPacket str) {
int size = Marshal.SizeOf(str);
byte[] arr = new byte[size];
IntPtr ptr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(str, ptr, true);
Marshal.Copy(ptr, arr, 0, size);
Marshal.FreeHGlobal(ptr);
return arr;
}
Ve geri dönüştürmek için:
CIFSPacket fromBytes(byte[] arr) {
CIFSPacket str = new CIFSPacket();
int size = Marshal.SizeOf(str);
IntPtr ptr = Marshal.AllocHGlobal(size);
Marshal.Copy(arr, 0, ptr, size);
str = (CIFSPacket)Marshal.PtrToStructure(ptr, str.GetType());
Marshal.FreeHGlobal(ptr);
return str;
}
Yapı, bir dize önce bu koymak gerekir
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
public string Buffer;
Ve SizeConst mümkün olan en büyük dize kadar büyük olduğundan emin olun.
Ve muhtemelen bunu okumalısın: http://msdn.microsoft.com/en-us/library/4ca6d5z7.aspx
Bunu Paylaş:
Bayt dönüştürmek için [nasıl] dize?...
Java nasıl insan okunabilir biçime bay...
Nasıl Java hex bir dize bir bayt dizis...
Bayt içine Java Dize dönüştürmek için ...
Nasıl Java UTF8 bayt dizileri için Diz...