日期:2014-05-18  浏览次数:21452 次

byte数组偏移赋值,急等!
因项目需要,需把几年前的一个VC++的通信服务端改成C#的,目前遇到一个问题就是实现应用层协议时,在byte[]偏移赋值时出了问题.
C++:
memcpy(pTempInfo->m_pData+offset,(const char*)com.Field(2).asString(),com.Field(2).asString().GetLength());
offset+=com.Field(2).asString().GetLength();

//phoneLength
sTemp=htons(com.Field(6).asString().GetLength());
memcpy(pTempInfo->m_pData+offset,&sTemp,sizeof(short));
offset+=sizeof(short);

直接偏移内存拷贝就行了
而C#:
m_data[m_offset] = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(m_length));
  m_offset += sizeof(long);

  m_data[m_offset] = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(PackageDirect.Server));
  m_offset += sizeof(short);

编译时报错,因为BitConverter.GetBytes返回的是byte[],而m_data[m_offset]表示的是byte,所以不可以赋值,请问大家有没有什么方法可以解决此问题?

------解决方案--------------------
msdn search 
Array.Copy
Marshal.Copy
Marshal