Skip to content

Commit 5a9e0c9

Browse files
committed
hex to int and vice versa conversion methods
1 parent e8875e8 commit 5a9e0c9

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Globalization;
2+
using Exiled.Permissions.Commands.Permissions;
3+
using JetBrains.Annotations;
4+
using SER.Code.ArgumentSystem.Arguments;
5+
using SER.Code.ArgumentSystem.BaseArguments;
6+
using SER.Code.Helpers.Exceptions;
7+
using SER.Code.MethodSystem.BaseMethods.Synchronous;
8+
using SER.Code.MethodSystem.MethodDescriptors;
9+
using SER.Code.ValueSystem;
10+
11+
namespace SER.Code.MethodSystem.Methods.NumberMethods;
12+
13+
[UsedImplicitly]
14+
public class HexToIntMethod : ReturningMethod<NumberValue>, ICanError, IAdditionalDescription
15+
{
16+
public override string Description => "Parses a hexadecimal number back to a number value";
17+
18+
public string AdditionalDescription => "Do not include the '#' symbol at the start of the text.";
19+
20+
public string[] ErrorReasons =>
21+
[
22+
"The provided string does not represent a hexadecimal number."
23+
];
24+
25+
public override Argument[] ExpectedArguments { get; } =
26+
[
27+
new TextArgument("hex number")
28+
];
29+
30+
public override void Execute()
31+
{
32+
ReturnValue = int.TryParse(
33+
Args.GetText("hex number"),
34+
NumberStyles.HexNumber,
35+
NumberFormatInfo.InvariantInfo,
36+
out var result)
37+
? result
38+
: throw new ScriptRuntimeError(this, ErrorReasons[0]);
39+
}
40+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using JetBrains.Annotations;
2+
using SER.Code.ArgumentSystem.Arguments;
3+
using SER.Code.ArgumentSystem.BaseArguments;
4+
using SER.Code.MethodSystem.BaseMethods.Synchronous;
5+
using SER.Code.ValueSystem;
6+
7+
namespace SER.Code.MethodSystem.Methods.NumberMethods;
8+
9+
[UsedImplicitly]
10+
public class IntToHexMethod : ReturningMethod<TextValue>
11+
{
12+
public override string Description => "Parses an integer into a hexadecimal number";
13+
14+
public override Argument[] ExpectedArguments { get; } =
15+
[
16+
new IntArgument("integer to parse")
17+
];
18+
19+
public override void Execute()
20+
{
21+
ReturnValue = Args.GetInt("integer to parse").ToString("X");
22+
}
23+
}

0 commit comments

Comments
 (0)