Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
HexEncode.cs
1using System.Numerics;
7
9{
14 {
23 : base(new ScriptNode[] { Data }, argumentTypes1Normal, Start, Length, Expression)
24 {
25 }
26
36 : base(new ScriptNode[] { Integer, NrBytes }, argumentTypes2Scalar, Start, Length, Expression)
37 {
38 }
39
43 public override string FunctionName => nameof(HexEncode);
44
48 public override string[] DefaultArgumentNames => new string[] { "Data" };
49
57 {
58 int c = Arguments.Length;
59
60 if (c == 1)
61 {
62 if (!(Arguments[0].AssociatedObjectValue is byte[] Bin))
63 throw new ScriptRuntimeException("Binary data expected.", this);
64
65 return new StringValue(Hashes.BinaryToString(Bin));
66 }
67 else
68 {
69 object Obj = Arguments[0].AssociatedObjectValue;
70 int NrBytes = (int)Expression.ToDouble(Arguments[1].AssociatedObjectValue);
71 if (NrBytes <= 0)
72 throw new ScriptRuntimeException("Number of bytes must be positive.", this);
73
74 string s;
75
76 if (Obj is double d)
77 {
78 if (d >= int.MinValue && d <= int.MaxValue)
79 s = ((int)d).ToString("x");
80 else if (d >= long.MinValue && d <= long.MaxValue)
81 s = ((long)d).ToString("x");
82 else
83 s = ((BigInteger)d).ToString("x");
84 }
85 else if (Obj is BigInteger i)
86 s = i.ToString("x");
87 else
88 throw new ScriptRuntimeException("Expected integer as first argument.", this);
89
90 int MaxLen = NrBytes << 1;
91 c = s.Length;
92
93 if (c > MaxLen)
94 throw new ScriptRuntimeException("Integer too long to fit.", this);
95 else if (c < MaxLen)
96 {
97 if (s[0] >= '8')
98 s = new string('f', MaxLen - c) + s;
99 else
100 s = new string('0', MaxLen - c) + s;
101 }
102
103 return new StringValue(s);
104 }
105 }
106 }
107}
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: HexEncode.cs:56
override string FunctionName
Name of the function
Definition: HexEncode.cs:43
override string[] DefaultArgumentNames
Default Argument names
Definition: HexEncode.cs:48
HexEncode(ScriptNode Integer, ScriptNode NrBytes, int Start, int Length, Expression Expression)
HexEncode(Data)
Definition: HexEncode.cs:35
HexEncode(ScriptNode Data, int Start, int Length, Expression Expression)
HexEncode(Data)
Definition: HexEncode.cs:22
Class managing a script expression.
Definition: Expression.cs:39
static double ToDouble(object Object)
Converts an object to a double value.
Definition: Expression.cs:4824
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
static readonly ArgumentType[] argumentTypes2Scalar
Two scalar parameters.
static readonly ArgumentType[] argumentTypes1Normal
One scalar parameter.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Integer-valued number.
Definition: Integer.cs:13
Collection of variables.
Definition: Variables.cs:25
Contains methods for simple hash calculations.
Definition: Hashes.cs:59
static string BinaryToString(byte[] Data)
Converts an array of bytes to a string with their hexadecimal representations (in lower case).
Definition: Hashes.cs:65
Basic interface for all types of elements.
Definition: IElement.cs:20