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