mirror of
https://git.lewd.wtf/PGR/ascnet
synced 2025-12-14 08:04:58 +01:00
Add project files.
This commit is contained in:
35
AscNet.Common/Util/Miscs.cs
Normal file
35
AscNet.Common/Util/Miscs.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
namespace AscNet.Common.Util
|
||||
{
|
||||
public static class Miscs
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user