ascnet/AscNet.Common/Util/Miscs.cs

44 lines
1.2 KiB
C#
Raw Normal View History

2023-10-06 13:29:27 +00:00
namespace AscNet.Common.Util
{
public static class Miscs
{
2023-11-11 07:28:21 +00:00
public static int ParseIntOr(string? s, int d)
{
if (int.TryParse(s, out var parsed))
{
return parsed;
}
return d;
}
2023-10-06 13:29:27 +00:00
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;
}
}
}