using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text.RegularExpressions;
public static void Main()
Console.WriteLine("Hello World");
public void Evaluate(string expression, Dictionary<string, object> parameters)
CustomExpressions Lib = new CustomExpressions();
expression = expression.Replace("[", string.Empty);
expression = expression.Replace("]", string.Empty);
var e = new DynamicExpression(expression, ExpressionLanguage.Csharp);
var context = new ExpressionContext { Owner = Lib };
context.Imports.Add(new Import(typeof(Math)));
foreach (var parameter in parameters)
context.Variables.Add(parameter.Key, parameter.Value);
var str = string.Join(",", parameters.Select(p => $"{p.Key}:{p.Value}"));
public class CustomExpressions
public int bcd(byte[] stream, int offset, int len)
var temp = new StringBuilder(len * 2);
for (var i = 0; i < len; i++)
int b = stream[offset++];
if ((byte)((b & 0xf0) >> 4) == 0x0f)
temp.Append(((b & 0xf0) >> 4));
if (((byte)(b & 0x0f) == 0x0f))
return int.Parse(temp.ToString().Substring(0, 1).Equals("0") ? temp.ToString().Substring(1) : temp.ToString());
public short int8(byte[] stream, int offset)
return unchecked((sbyte)stream[offset]);
public short sint8(byte[] stream, int offset)
return unchecked((sbyte)stream[offset]);
public short uint8(byte[] stream, int offset)
public short int16(byte[] stream, int offset)
return leBitConverter.ToInt16(stream, offset);
public short int16le(byte[] stream, int offset)
return leBitConverter.ToInt16(stream, offset);
public short int16be(byte[] stream, int offset)
return beBitConverter.ToInt16(stream, offset);
public short sint16(byte[] stream, int offset)
return leBitConverter.ToInt16(stream, offset);
public short sint16le(byte[] stream, int offset)
return leBitConverter.ToInt16(stream, offset);
public short sint16be(byte[] stream, int offset)
return beBitConverter.ToInt16(stream, offset);
public ushort uint16(byte[] stream, int offset)
return leBitConverter.ToUInt16(stream, offset);
public ushort uint16le(byte[] stream, int offset)
return leBitConverter.ToUInt16(stream, offset);
public ushort uint16be(byte[] stream, int offset)
return beBitConverter.ToUInt16(stream, offset);
public int int32(byte[] stream, int offset)
return leBitConverter.ToInt32(stream, offset);
public int int32le(byte[] stream, int offset)
return leBitConverter.ToInt32(stream, offset);
public int int32be(byte[] stream, int offset)
return beBitConverter.ToInt32(stream, offset);
public int sint32(byte[] stream, int offset)
return leBitConverter.ToInt32(stream, offset);
public int sint32le(byte[] stream, int offset)
return leBitConverter.ToInt32(stream, offset);
public int sint32be(byte[] stream, int offset)
return beBitConverter.ToInt32(stream, offset);
public uint uint32(byte[] stream, int offset)
return leBitConverter.ToUInt32(stream, offset);
public uint uint32le(byte[] stream, int offset)
return leBitConverter.ToUInt32(stream, offset);
public uint uint32be(byte[] stream, int offset)
return beBitConverter.ToUInt32(stream, offset);
public long int64(byte[] stream, int offset)
return leBitConverter.ToInt64(stream, offset);
public long int64le(byte[] stream, int offset)
return leBitConverter.ToInt64(stream, offset);
public long int64be(byte[] stream, int offset)
return beBitConverter.ToInt64(stream, offset);
public long sint64(byte[] stream, int offset)
return leBitConverter.ToInt64(stream, offset);
public long sint64le(byte[] stream, int offset)
return leBitConverter.ToInt64(stream, offset);
public long sint64be(byte[] stream, int offset)
return beBitConverter.ToInt64(stream, offset);
public ulong uint64(byte[] stream, int offset)
return leBitConverter.ToUInt64(stream, offset);
public ulong uint64le(byte[] stream, int offset)
return leBitConverter.ToUInt64(stream, offset);
public ulong uint64be(byte[] stream, int offset)
return beBitConverter.ToUInt64(stream, offset);
public float floatbe(byte[] stream, int offset)
return beBitConverter.ToSingle(stream, offset);
public float floatle(byte[] stream, int offset)
return leBitConverter.ToSingle(stream, offset);
public double doublebe(byte[] stream, int offset)
return beBitConverter.ToDouble(stream, offset);
public double doublele(byte[] stream, int offset)
return leBitConverter.ToDouble(stream, offset);
public string ascii(byte[] stream, int offset, int len)
return Regex.Replace(new string(Encoding.ASCII.GetChars(stream, offset, len)), "\x00", string.Empty);
public string s7scldt(byte[] stream, int offset)
int year = stream[offset + 0].AsBcd() + 2000;
int month = stream[offset + 1].AsBcd();
int day = stream[offset + 2].AsBcd();
int hour = stream[offset + 3].AsBcd();
int minute = stream[offset + 4].AsBcd();
int second = stream[offset + 5].AsBcd();
string milisecString1 = stream[offset + 6].AsBcd().ToString(CultureInfo.InvariantCulture);
int milisecByte2 = stream[offset + 7] >> 4;
string milisecString2 = ((byte)(milisecByte2)).AsBcd().ToString(CultureInfo.InvariantCulture);
double milisecond = short.Parse(milisecString1 + milisecString2);
DateTime dTimePrlim = new DateTime(year, month, day, hour, minute, second, DateTimeKind.Utc);
DateTime dT = dTimePrlim.AddMilliseconds(milisecond);
return dT.ToString(CultureInfo.InvariantCulture);
public static class NumericExtensions
public static bool Between(this float value, float a, float b)
return value >= a && value <= b;
public static bool Between(this decimal value, decimal a, decimal b)
return value >= a && value <= b;
public static bool Between(this byte value, byte a, byte b)
return value >= a && value <= b;
public static bool Between(this short value, short a, short b)
return value >= a && value <= b;
public static bool Between(this ushort value, ushort a, ushort b)
return value >= a && value <= b;
public static bool Between(this int value, int a, int b)
return value >= a && value <= b;
public static bool Between(this uint value, uint a, uint b)
return value >= a && value <= b;
public static bool Between(this double value, double a, double b)
return value >= a && value <= b;
public static bool Between(this long value, long a, long b)
return value >= a && value <= b;
public static bool Between(this ulong value, ulong a, ulong b)
return value >= a && value <= b;
public static int Ror(this int input, int sizeInBits, int places)
return ((input >> places) | (input << (sizeInBits - places))) & (int)Math.Pow(2, sizeInBits) - 1;
public static int Rol(this int input, int sizeInBits, int places)
return ((input << places) | (input >> (sizeInBits - places))) & (int)Math.Pow(2, sizeInBits) - 1;
public static bool AlmostEquals(this double double1, double double2, double precision)
return (Math.Abs(double1 - double2) <= precision);
public static bool AlmostEquals(this float float1, float float2, float precision)
return (Math.Abs(float1 - float2) <= precision);
public static int AsBcd(this byte value)