forked from PGR/ascnet
44 lines
1.2 KiB
C#
44 lines
1.2 KiB
C#
namespace AscNet.Common.Util
|
|
{
|
|
public static class Miscs
|
|
{
|
|
public static int ParseIntOr(string? s, int d)
|
|
{
|
|
if (int.TryParse(s, out var parsed))
|
|
{
|
|
return parsed;
|
|
}
|
|
return d;
|
|
}
|
|
public static byte[] HexStringToByteArray(string hex)
|
|
{
|
|
if (hex.Length % 2 == 1)
|
|
throw new Exception("The binary key cannot have an odd number of digits");
|
|
|
|
byte[] arr = new byte[hex.Length >> 1];
|
|
|
|
for (int i = 0; i < hex.Length >> 1; ++i)
|
|
{
|
|
arr[i] = (byte)((GetHexVal(hex[i << 1]) << 4) + (GetHexVal(hex[(i << 1) + 1])));
|
|
}
|
|
|
|
return arr;
|
|
}
|
|
|
|
private static int GetHexVal(char hex)
|
|
{
|
|
int val = (int)hex;
|
|
//For uppercase A-F letters:
|
|
//return val - (val < 58 ? 48 : 55);
|
|
//For lowercase a-f letters:
|
|
//return val - (val < 58 ? 48 : 87);
|
|
//Or the two combined, but a bit slower:
|
|
return val - (val < 58 ? 48 : (val < 97 ? 55 : 87));
|
|
}
|
|
public static byte ToByte(this bool val)
|
|
{
|
|
return val ? (byte)1 : (byte)0;
|
|
}
|
|
}
|
|
}
|