2016-09-26 23:43:52 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
2017-01-08 07:54:09 +00:00
|
|
|
|
namespace PKHeX.Core
|
2016-09-26 23:43:52 +00:00
|
|
|
|
{
|
2017-01-08 07:54:09 +00:00
|
|
|
|
public static class BigEndian
|
2016-09-26 23:43:52 +00:00
|
|
|
|
{
|
2017-01-08 07:54:09 +00:00
|
|
|
|
public static uint ToUInt32(byte[] data, int offset)
|
2016-09-26 23:43:52 +00:00
|
|
|
|
{
|
|
|
|
|
int val = 0;
|
|
|
|
|
val |= data[offset + 0] << 24;
|
|
|
|
|
val |= data[offset + 1] << 16;
|
|
|
|
|
val |= data[offset + 2] << 8;
|
2019-09-11 05:07:50 +00:00
|
|
|
|
val |= data[offset + 3];
|
2016-09-26 23:43:52 +00:00
|
|
|
|
return (uint)val;
|
|
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
|
2017-01-08 07:54:09 +00:00
|
|
|
|
public static ushort ToUInt16(byte[] data, int offset)
|
2016-09-26 23:43:52 +00:00
|
|
|
|
{
|
|
|
|
|
int val = 0;
|
|
|
|
|
val |= data[offset + 0] << 8;
|
2019-09-11 05:07:50 +00:00
|
|
|
|
val |= data[offset + 1];
|
2016-09-26 23:43:52 +00:00
|
|
|
|
return (ushort)val;
|
|
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
|
2017-01-08 07:54:09 +00:00
|
|
|
|
public static int ToInt32(byte[] data, int offset)
|
2016-09-26 23:43:52 +00:00
|
|
|
|
{
|
|
|
|
|
int val = 0;
|
|
|
|
|
val |= data[offset + 0] << 24;
|
|
|
|
|
val |= data[offset + 1] << 16;
|
|
|
|
|
val |= data[offset + 2] << 8;
|
2019-09-11 05:07:50 +00:00
|
|
|
|
val |= data[offset + 3];
|
2016-09-26 23:43:52 +00:00
|
|
|
|
return val;
|
|
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
|
2017-01-08 07:54:09 +00:00
|
|
|
|
public static short ToInt16(byte[] data, int offset)
|
2016-09-26 23:43:52 +00:00
|
|
|
|
{
|
|
|
|
|
int val = 0;
|
|
|
|
|
val |= data[offset + 0] << 8;
|
2019-09-11 05:07:50 +00:00
|
|
|
|
val |= data[offset + 1];
|
2016-09-26 23:43:52 +00:00
|
|
|
|
return (short)val;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-08 07:54:09 +00:00
|
|
|
|
public static byte[] GetBytes(int value)
|
2016-09-26 23:43:52 +00:00
|
|
|
|
{
|
|
|
|
|
return Invert(BitConverter.GetBytes(value));
|
|
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
|
2017-01-08 07:54:09 +00:00
|
|
|
|
public static byte[] GetBytes(short value)
|
2016-09-26 23:43:52 +00:00
|
|
|
|
{
|
|
|
|
|
return Invert(BitConverter.GetBytes(value));
|
|
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
|
2017-01-08 07:54:09 +00:00
|
|
|
|
public static byte[] GetBytes(uint value)
|
2016-09-26 23:43:52 +00:00
|
|
|
|
{
|
|
|
|
|
return Invert(BitConverter.GetBytes(value));
|
|
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
|
2017-01-08 07:54:09 +00:00
|
|
|
|
public static byte[] GetBytes(ushort value)
|
2016-09-26 23:43:52 +00:00
|
|
|
|
{
|
|
|
|
|
return Invert(BitConverter.GetBytes(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static byte[] Invert(byte[] data)
|
|
|
|
|
{
|
2019-02-15 19:46:02 +00:00
|
|
|
|
int i = 0;
|
|
|
|
|
int j = 0 + data.Length - 1;
|
|
|
|
|
while (i < j)
|
|
|
|
|
{
|
|
|
|
|
var temp = data[i];
|
|
|
|
|
data[i] = data[j];
|
|
|
|
|
data[j] = temp;
|
|
|
|
|
i++;
|
|
|
|
|
j--;
|
|
|
|
|
}
|
|
|
|
|
return data;
|
2016-09-26 23:43:52 +00:00
|
|
|
|
}
|
2017-02-08 23:51:18 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a 32-bit signed integer converted from bytes in a Binary Coded Decimal format byte array.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input">Input byte array to read from.</param>
|
|
|
|
|
/// <param name="offset">Offset to start reading at.</param>
|
|
|
|
|
/// <param name="length">Length of array to read.</param>
|
|
|
|
|
public static int BCDToInt32(byte[] input, int offset, int length)
|
|
|
|
|
{
|
|
|
|
|
int result = 0;
|
|
|
|
|
for (int i = offset; i < offset + length; i++)
|
|
|
|
|
{
|
|
|
|
|
byte p = input[i];
|
|
|
|
|
result *= 100;
|
|
|
|
|
result += 10 * (p >> 4);
|
|
|
|
|
result += p & 0xf;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
|
2017-02-08 23:51:18 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the specified 32-bit signed integer value as an array of Binary Coded Decimal format bytes.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input">32-bit signed integer to convert.</param>
|
|
|
|
|
/// <param name="size">Desired size of returned array.</param>
|
|
|
|
|
public static byte[] Int32ToBCD(int input, int size)
|
|
|
|
|
{
|
|
|
|
|
byte[] result = new byte[size];
|
|
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
|
{
|
|
|
|
|
int p = input%100;
|
|
|
|
|
input /= 100;
|
|
|
|
|
result[size - i - 1] = (byte)(p/10 << 4 | p%10);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2020-09-01 23:05:52 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a 16-bit signed integer converted from bytes in a Binary Coded Decimal format byte array.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>Little Endian instead of Big Endian</remarks>
|
|
|
|
|
/// <param name="input">Input byte array to read from.</param>
|
|
|
|
|
/// <param name="offset">Offset to start reading at.</param>
|
|
|
|
|
/// <param name="length">Length of array to read.</param>
|
|
|
|
|
public static int BCDToInt32_LE(byte[] input, int offset, int length)
|
|
|
|
|
{
|
|
|
|
|
int result = 0;
|
|
|
|
|
for (int i = offset + length - 1; i >= offset; i--)
|
|
|
|
|
{
|
|
|
|
|
byte p = input[i];
|
|
|
|
|
result *= 100;
|
|
|
|
|
result += 10 * (p >> 4);
|
|
|
|
|
result += p & 0xf;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the specified 32-bit signed integer value as an array of Binary Coded Decimal format bytes.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>Little Endian instead of Big Endian</remarks>
|
|
|
|
|
/// <param name="input">32-bit signed integer to convert.</param>
|
|
|
|
|
/// <param name="size">Desired size of returned array.</param>
|
|
|
|
|
public static byte[] Int32ToBCD_LE(int input, int size)
|
|
|
|
|
{
|
|
|
|
|
byte[] result = new byte[size];
|
|
|
|
|
for (int i = size - 1; i >= 0; i--)
|
|
|
|
|
{
|
|
|
|
|
int p = input % 100;
|
|
|
|
|
input /= 100;
|
|
|
|
|
result[size - i - 1] = (byte)(p / 10 << 4 | p % 10);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2016-09-26 23:43:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|