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