rendered paste body public static object ToNET(char Type, int Offset, int Length, int Decimal, byte[] Data)
{
switch (Type)
{
case 'C':
{
return ExtractString(Offset, Length, Data).Trim();
}
case 'N':
{
if (Decimal == 0)
{
int Result = 0;
int.TryParse(ExtractString(Offset, Length, Data), out Result);
return Result;
}
else
{
double Result = 0;
double.TryParse(ExtractString(Offset, Length, Data), out Result);
return Result;
}
}
case 'L':
{
switch ((char)Data[Offset])
{
case 'Y':
case 'y':
case 'T':
case 't':
{
return true;
}
case 'N':
case 'n':
case 'F':
case 'f':
{
return false;
}
case '?':
default:
{
return null;
}
}
}
case 'D':
{
string StrDate = ExtractString(Offset, Length, Data).Trim();
if (StrDate != "")
{
return DateTime.ParseExact(StrDate, "yyyymmdd", System.Globalization.DateTimeFormatInfo.CurrentInfo);
}
else
{
return null;
}
}
case 'M':
{
int Result = 0;
int.TryParse(ExtractString(Offset, 10, Data).Trim(), out Result);
return Result;
}
case 'T':
{
int IntDate = BitConverter.ToInt32(Data, Offset);
int IntTime = BitConverter.ToInt32(Data, Offset + 4);
IntDate -= MagicDate;
if (IntDate > 0)
{
DateTime dt = new DateTime(1, 1, 1, 0, 0, 0);
dt = dt.AddMilliseconds(IntTime);
dt = dt.AddDays(IntDate);
return dt;
}
else
{
return null;
}
}
case 'I':
{
return BitConverter.ToInt32(Data, Offset);
}
default:
return BitConverter.ToString(Data, Offset, Length);
}
}
public static byte[] ToDBF(char Type, int Length, int Decimal, object Value)
{
byte[] Data = new byte[Length];
switch (Type)
{
case 'C':
{
ASCIIEncoding.ASCII.GetBytes((string)Value + (new string(' ', Length - ((string)Value).Length))).CopyTo(Data, 0);
break;
}
case 'N':
{
string value;
if (Decimal == 0)
{
value = ((int)Value).ToString();
}
else
{
value = ((double)Value).ToString();
}
if (value == "0")
{
value = new string(' ', Length);
}
else
{
value = value + new string(' ', Length - value.Length);
}
ASCIIEncoding.ASCII.GetBytes(value).CopyTo(Data, 0);
break;
}
case 'L':
{
if (Value != null)
{
Data[0] = (byte)((bool)Value ? 'T' : 'F');
}
else
{
Data[0] = 0x2A;
}
break;
}
case 'D':
{
ASCIIEncoding.ASCII.GetBytes(((DateTime)Value).ToString("yyyymmdd")).CopyTo(Data, 0);
break;
}
case 'M':
{
break;
}
case 'T':
{
if (Value != null)
{
DateTime dt = (DateTime)Value;
TimeSpan ts = dt.Subtract(new DateTime(1, 1, 1));
int IntDate = ts.Days + MagicDate;
int IntTime = (int)(ts.TotalMilliseconds - (ts.Days * (TimeSpan.TicksPerDay / 10000)));
BitConverter.GetBytes(IntDate).CopyTo(Data, 0);
BitConverter.GetBytes(IntTime).CopyTo(Data, 4);
}
break;
}
case 'I':
{
BitConverter.GetBytes((int)Value).CopyTo(Data, 0);
break;
}
default:
{
break;
}
}
return Data;
}
}