公司的一些封包的程序有些看不懂,请高手指点
/// <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),
(byte)(val_int> > 8*6),(byte)(val_int> > 8*7)
};
}
else if (type == typeof(uint))
{
uint val_int = (uint)val;
ret = new byte[]{
(byte)val_int,(byte)(val_int> > 8),(byte)(val_int> > 16),(byte)(val_int> > 24)
};
}
else if (type == typeof(int))
{
uint val_int = (uint)((int)val);
ret = new byte[]{
(byte)val_int,(byte)(val_int> > 8),(byte)(val_int> > 16),(byte)(val_int> > 24)
};
}
else if(type == typeof(short))
{
short val_int = (short)val;
ret = new byte[]{
(byte)val_int,(byte)(val_int> > 8)
};
}
else if(type == typeof(ushort))
{
ushort val_int = (ushort)val;
ret = new byte[]{
(byte)val_int,(byte)(val_int> > 8)
};
}
else if (type == typeof(byte))
{
byte val_int = (byte)val;
ret = new byte[]{(byte)val_int};
}
else if (type == typeof(sbyte))
{
byte val_int = (byte)((sbyte)val);
ret = new byte[]{(byte)val_int};
}
else
throw new TypeException(val.GetType().ToString(), "ulong,long,uint,int,byte,sbyte ");
return ret;
}
case C_Global.CEnum.TagFormat.TLV_MONEY:
{
if (val.GetType() != typeof(double))
throw new TypeException(val.GetType().ToString(),typeof(double).ToString());
uint val_int = (uint)((double)val);
uint val_dec = (uint)(((double)val - val_int)*100);
return new byte[]{
(byte)(val_int> > 16),(byte)(val_int> > 8),(byte)val_int,(byte)val_dec
};
}
case C_Global.CEnum.TagFormat.TLV_NUMBER:
{
if (val.GetType() != typeof(string))
throw new TypeException(val.GetType().ToString(),typeof(string).ToString());
string val_string = (string)val;
if (!System.Text.RegularExpressions.Regex.IsMatch(val_string,@ "^\d{11,12}$ ",System.Text.RegularExpressions.RegexOptions.Compiled))
throw new ValueException(val_string, "11-12位的数字 ");
byte[] ret = new byte[1 + (val_string.Length + 1)/2];
ret[0] = (byte)val_string.Length;
for ( int i = 0; i < val_string.Length ;i++)
{
if (i %2 == 0)
{
ret[1 + i/2] += (byte)(int.Parse(val_string.Substring(i,1)) < <4);
}
else
{
ret[1 + i/2] += (byte)(int.Parse(val_string.Substring(i,1)));
}
}
return ret;
}
case C_Global.CEnum.TagFormat.TLV_STRING:
{
if (val.GetType() != typeof(string))
throw new TypeException(val.GetType().ToString(),typeof(string).ToString());
return System.Text.Encoding.Default.GetBytes((string)val);
}
case C_Global.CEnum.TagFormat.TLV_TIME:
{
if (val.GetType() != typeof(System.DateTime))
throw new TypeException(val.GetType().ToString(),typeof(System.DateTime).ToString());
int hour = ((DateTime)val).Hour;
int minute = ((DateTime)val).Minute;
int second = ((DateTime)val).Second;
return new byte[]{
(byte)hour,(byte)minute,(byte)second
};
}
case C_Global.CEnum.TagFormat.TLV_TIMESTAMP:
{
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;
int hour = ((DateTime)val).Hour;
int minute = ((DateTime)val).Minute;
int second = ((DateTime)val).Second;
return new byte[]{
(byte)year,(byte)month,(byte)day,(byte)hour,(byte)minute,(byte)second
};
}
case C_Global.CEnum.TagFormat.TLV_BOOLEAN:
{
if (val.GetType() != typeof(bool))
throw new TypeException(val.GetType().ToString(),typeof(bool).ToString());
bool val_int = (bool)val;
return new byte[]{Convert.ToByte(val_int)};
}
default:
throw new ValueException(val.ToString(), "TagFormat定义的所有类型 ");
}
}
}
}
这是公司socket 中Packet的TLV_STRUSTURE
发觉对其中的一些不能很好理解,其他高手又挺忙,不好问
请大家不吝赐教
[解决办法]
too long~~ft...
[解决办法]
代码帖的有点乱
[解决办法]
代码不你懂?代码好象不太难懂啊,是不是那些 算法你看不懂!!
[解决办法]
好长,慢慢看