2018-08-13 18:13:01 -06:00
|
|
|
|
using System;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
2018-08-16 17:47:36 -06:00
|
|
|
|
namespace Ryujinx.HLE.Utilities
|
2018-08-13 18:13:01 -06:00
|
|
|
|
{
|
|
|
|
|
static class StringUtils
|
|
|
|
|
{
|
|
|
|
|
public static byte[] GetFixedLengthBytes(string InputString, int Size, Encoding Encoding)
|
|
|
|
|
{
|
|
|
|
|
InputString = InputString + "\0";
|
|
|
|
|
|
2018-08-14 16:02:42 -06:00
|
|
|
|
int BytesCount = Encoding.GetByteCount(InputString);
|
2018-08-13 18:13:01 -06:00
|
|
|
|
|
|
|
|
|
byte[] Output = new byte[Size];
|
|
|
|
|
|
2018-08-14 16:02:42 -06:00
|
|
|
|
if (BytesCount < Size)
|
2018-08-13 18:13:01 -06:00
|
|
|
|
{
|
2018-08-14 16:02:42 -06:00
|
|
|
|
Encoding.GetBytes(InputString, 0, InputString.Length, Output, 0);
|
2018-08-13 18:13:01 -06:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int NullSize = Encoding.GetByteCount("\0");
|
|
|
|
|
|
|
|
|
|
Output = Encoding.GetBytes(InputString);
|
|
|
|
|
|
|
|
|
|
Array.Resize(ref Output, Size - NullSize);
|
|
|
|
|
|
|
|
|
|
Output = Output.Concat(Encoding.GetBytes("\0")).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static byte[] HexToBytes(string HexString)
|
|
|
|
|
{
|
2018-08-14 16:02:42 -06:00
|
|
|
|
//Ignore last charactor if HexLength % 2 != 0.
|
2018-08-13 18:13:01 -06:00
|
|
|
|
int BytesInHex = HexString.Length / 2;
|
|
|
|
|
|
|
|
|
|
byte[] Output = new byte[BytesInHex];
|
|
|
|
|
|
|
|
|
|
for (int Index = 0; Index < BytesInHex; Index++)
|
|
|
|
|
{
|
2018-08-14 16:02:42 -06:00
|
|
|
|
Output[Index] = byte.Parse(HexString.Substring(Index * 2, 2), NumberStyles.HexNumber);
|
2018-08-13 18:13:01 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Output;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|