Hex
Hex.Print<I>(value, leadingZeroes = false) prints any binary integer into a string.
WriteLine(Hex.Print<BigInteger>(123)); // "7B"
WriteLine(Hex.Print<byte>(123)); // "7B"
WriteLine(Hex.Print<int>(123, leadingZeroes: false)); // "7B"
WriteLine(Hex.Print<int>(123, leadingZeroes: true)); // "0000007B"
WriteLine(Hex.Print<long>(-1)); // "FFFFFFFFFFFFFFFF"
Hex.Parse<I>(span) parses hexadecimal string into any binary integer. Note that BigInteger can contain 16GB digits.
BigInteger value = Hex.Parse<BigInteger>("1234abcd76859493fff12313");
WriteLine(value); // 5634405634195673405673513747
int value2 = Hex.Parse<int>("1000");
WriteLine(value2); // 4096
Hex.TryParse<I>(span, out I value) tries to parse hexadecimal string into integer.
if (Hex.TryParse<int>("1000", out int value, out int digitsRead))
WriteLine(value); // 4096
Hex.PrintFromBytes(bytes) prints bytes into hexadecimal string.
byte[] data = new byte[] { 0, 1, 0, 0 };
WriteLine(Hex.PrintFromBytes(data)); // "00010000"
Hex.TryParseToBytes(span, out bytes) parses hexadecimal string into bytes.
if (Hex.TryParseToBytes("FF", out byte[]? bytes))
WriteLine(bytes[0]); // 255
[Hex] indicates that string contains hexadecimal content.
public record MyRecord100
{
[Hex]
public string X = "0100";
[Hex]
public string Y = "0100";
[Hex]
public string Z = "0100";
}
Full Example
Full example
using System.Numerics;
using Avalanche.Utilities;
using static System.Console;
public class hex
{
public static void Run()
{
{
// <01>
WriteLine(Hex.Print<BigInteger>(123)); // "7B"
WriteLine(Hex.Print<byte>(123)); // "7B"
WriteLine(Hex.Print<int>(123, leadingZeroes: false)); // "7B"
WriteLine(Hex.Print<int>(123, leadingZeroes: true)); // "0000007B"
WriteLine(Hex.Print<long>(-1)); // "FFFFFFFFFFFFFFFF"
// </01>
}
{
// <02>
BigInteger value = Hex.Parse<BigInteger>("1234abcd76859493fff12313");
WriteLine(value); // 5634405634195673405673513747
int value2 = Hex.Parse<int>("1000");
WriteLine(value2); // 4096
// </02>
}
{
// <03>
if (Hex.TryParse<int>("1000", out int value, out int digitsRead))
WriteLine(value); // 4096
// </03>
}
{
// <04>
byte[] data = new byte[] { 0, 1, 0, 0 };
WriteLine(Hex.PrintFromBytes(data)); // "00010000"
// </04>
}
{
// <05>
if (Hex.TryParseToBytes("FF", out byte[]? bytes))
WriteLine(bytes[0]); // 255
// </05>
}
}
}
// <06>
public record MyRecord100
{
[Hex]
public string X = "0100";
[Hex]
public string Y = "0100";
[Hex]
public string Z = "0100";
}
// </06>