makes the byteToHex and HexToByte simple

This commit is contained in:
jonnysp 2018-09-27 15:01:39 +02:00
parent e357c753f5
commit 75864b7677

View file

@ -142,121 +142,23 @@ namespace LibHac
return fullFile.Substring(fullDirectory.Length + 1); return fullFile.Substring(fullDirectory.Length + 1);
} }
private static bool TryHexToInt(char c, out int value)
{
switch (c)
{
case '0':
value = 0; break;
case '1':
value = 1; break;
case '2':
value = 2; break;
case '3':
value = 3; break;
case '4':
value = 4; break;
case '5':
value = 5; break;
case '6':
value = 6; break;
case '7':
value = 7; break;
case '8':
value = 8; break;
case '9':
value = 9; break;
case 'a':
case 'A':
value = 10; break;
case 'b':
case 'B':
value = 11; break;
case 'c':
case 'C':
value = 12; break;
case 'd':
case 'D':
value = 13; break;
case 'e':
case 'E':
value = 14; break;
case 'f':
case 'F':
value = 15; break;
default:
value = 0;
return false;
}
return true;
}
private static readonly byte[,] ByteLookup = {
{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
{0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0}
};
public static byte[] ToBytes(this string input) public static byte[] ToBytes(this string input)
{ {
var result = new byte[(input.Length + 1) >> 1]; return Enumerable.Range(0, input.Length)
int lastcell = result.Length - 1; .Where(x => x % 2 == 0)
int lastchar = input.Length - 1; .Select(x => Convert.ToByte(input.Substring(x, 2), 16))
for (int i = 0; i < input.Length; i++) .ToArray();
{
if (!TryHexToInt(input[lastchar - i], out int hexInt))
{
throw new FormatException($"Unrecognized hex char {input[lastchar - i]}");
}
result[lastcell - (i >> 1)] |= ByteLookup[i & 1, hexInt];
}
return result;
} }
public static bool TryToBytes(this string input, out byte[] bytes) public static bool TryToBytes(this string input, out byte[] bytes)
{ {
var result = new byte[(input.Length + 1) >> 1]; bytes = input.ToBytes();
int lastcell = result.Length - 1; return bytes.Length == (input.Length / 2);
int lastchar = input.Length - 1;
for (int i = 0; i < input.Length; i++)
{
if (!TryHexToInt(input[lastchar - i], out int hexInt))
{
bytes = null;
return false;
}
result[lastcell - (i >> 1)] |= ByteLookup[i & 1, hexInt];
}
bytes = result;
return true;
}
private static readonly uint[] Lookup32 = CreateLookup32();
private static uint[] CreateLookup32()
{
var result = new uint[256];
for (int i = 0; i < 256; i++)
{
string s = i.ToString("X2");
result[i] = s[0] + ((uint)s[1] << 16);
}
return result;
} }
public static string ToHexString(this byte[] bytes) public static string ToHexString(this byte[] bytes)
{ {
var lookup32 = Lookup32; return string.Concat(bytes.Select(b => b.ToString("X2")));
var result = new char[bytes.Length * 2];
for (int i = 0; i < bytes.Length; i++)
{
var val = lookup32[bytes[i]];
result[2 * i] = (char)val;
result[2 * i + 1] = (char)(val >> 16);
}
return new string(result);
} }
public static long MediaToReal(long media) public static long MediaToReal(long media)