using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StardustCore.StardustMath
{
///
/// Base class for hex representation.
///
public class Hex
{
///
/// The hex value represented as a string.
///
public string hexValue;
///
/// Default constructor.
///
public Hex()
{
}
///
/// Empty Constructor;
///
///
public Hex(string HexValue)
{
}
///
/// Verifies that the hex is of the specified length.
///
///
public virtual bool verifyHexLength()
{
return true;
}
///
/// Trims the hex value by removing the leading 0x;
///
///
public virtual string trimHex()
{
return this.hexValue.Split('x')[1];
}
///
/// A virtual function to be overriden.
///
///
public virtual List getBytes()
{
return new List();
}
///
/// Converts a Hex byte (represented as a length two string) to an int equal or less than 255. Ex ff=255;
///
/// The length two value to be converted.
///
public virtual int convertHexByteTo255Int(string value)
{
int val1 = convertHexValueToInt(value[0]);
int val2 = convertHexValueToInt(value[1]);
val1 *= 16;
val2 *= 16;
return val1 + val2;
}
///
/// Converts a hex char to an int.
///
///
///
public virtual int convertHexValueToInt(char c)
{
if (c == 'a') return 10;
else if (c == 'b') return 11;
else if (c == 'c') return 12;
else if (c == 'd') return 13;
else if (c == 'e') return 14;
else if (c == 'f') return 15;
else return Convert.ToInt32(c);
}
///
/// Gets the associated byte from the hex positioning.
///
///
///
public virtual string getByte(int index)
{
string hex = this.trimHex();
int val = index * 2 + 1;
char str1 = hex[index * 2];
char str2 = hex[val];
return (str1.ToString() + str2.ToString());
}
}
}