2018-12-30 18:00:05 +08:00
|
|
|
using System;
|
2018-02-22 05:41:43 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2018-02-24 05:55:51 +08:00
|
|
|
namespace StardustCore.StardustMath
|
2018-02-22 05:41:43 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Base class for hex representation.</summary>
|
2018-02-22 05:41:43 +08:00
|
|
|
public class Hex
|
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>The hex value represented as a string.</summary>
|
2018-02-22 05:41:43 +08:00
|
|
|
public string hexValue;
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Default constructor.</summary>
|
|
|
|
public Hex() { }
|
2018-02-22 05:41:43 +08:00
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Construct an instance.</summary>
|
2018-02-22 05:41:43 +08:00
|
|
|
/// <param name="HexValue"></param>
|
2018-12-30 18:00:05 +08:00
|
|
|
public Hex(string HexValue) { }
|
2018-02-22 05:41:43 +08:00
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Verifies that the hex is of the specified length.</summary>
|
2018-02-22 05:41:43 +08:00
|
|
|
public virtual bool verifyHexLength()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Trims the hex value by removing the leading 0x;</summary>
|
2018-02-22 05:41:43 +08:00
|
|
|
public virtual string trimHex()
|
|
|
|
{
|
|
|
|
return this.hexValue.Split('x')[1];
|
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>A virtual function to be overriden.</summary>
|
2018-02-22 05:41:43 +08:00
|
|
|
public virtual List<string> getBytes()
|
|
|
|
{
|
|
|
|
return new List<string>();
|
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Converts a Hex byte (represented as a length two string) to an int equal or less than 255. Ex ff=255;</summary>
|
2018-02-22 05:41:43 +08:00
|
|
|
/// <param name="value">The length two value to be converted.</param>
|
|
|
|
public virtual int convertHexByteTo255Int(string value)
|
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
int val1 = this.convertHexValueToInt(value[0]);
|
|
|
|
int val2 = this.convertHexValueToInt(value[1]);
|
2018-02-22 05:41:43 +08:00
|
|
|
val1 *= 16;
|
|
|
|
val2 *= 16;
|
|
|
|
return val1 + val2;
|
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Converts a hex char to an int.</summary>
|
2018-02-22 05:41:43 +08:00
|
|
|
/// <param name="c"></param>
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Gets the associated byte from the hex positioning.</summary>
|
2018-02-22 05:41:43 +08:00
|
|
|
/// <param name="index"></param>
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|