- 爱易网页
-
C#教程
- 公司的一些封包的程序有些看不懂,请
日期:2014-05-19 浏览次数:20745 次
公司的一些封包的程序有些看不懂,请高手指点
/// <summary>
/// 转换成byte
/// </summary>
/// <param name= "tagformat "> 数据类型 </param>
/// <param name= "val "> 值 </param>
/// <returns> </returns>
public static byte[] ValueToByteArray(C_Global.CEnum.TagFormat tagformat,object val)
{
switch(tagformat)
{
case C_Global.CEnum.TagFormat.TLV_DATE:
{
if (val.GetType() != typeof(System.DateTime))
throw new TypeException(val.GetType().ToString(),typeof(System.DateTime).ToString());
int year = ((DateTime)val).Year - 1900;
int month = ((DateTime)val).Month;
int day = ((DateTime)val).Day;
return new byte[]{
(byte)year,(byte)month,(byte)day
};
}
case C_Global.CEnum.TagFormat.TLV_EXTEND:
{
if (val.GetType() != typeof(Packet_Body))
throw new TypeException(val.GetType().ToString(), typeof(Packet_Body).ToString());
else if (((Packet_Body)val).m_Status != CEnum.Body_Status.MSG_STRUCT_OK)
throw new TypeException(((Packet_Body)val).m_Status.ToString(), CEnum.Body_Status.MSG_STRUCT_OK.ToString());
return ((Packet_Body)val).ToByteArray();
}
case C_Global.CEnum.TagFormat.TLV_INTEGER:
{
byte[] ret = null;
System.Type type = val.GetType();
if (type == typeof(ulong))
{
ulong val_int = (ulong)val;
ret = new byte[]{
(byte)val_int,(byte)(val_int> > 8),(byte)(val_int> > 8*2),
(byte)(val_int> > 8*3),(byte)(val_int> > 8*4),(byte)(val_int> > 8*5),
(byte)(val_int> > 8*6),(byte)(val_int> > 8*7)
};
}
else if (type == typeof(long))
{
ulong val_int = (ulong)((long)val);
ret = new byte[]{
(byte)val_int,(byte)(val_int> > 8),(byte)(val_int> > 8*2),
(byte)(val_int> > 8*3),(byte)(val_int> > 8*4),(byte)(val_int> > 8*5),