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
|
|
|
|
{
|
2021-05-14 19:23:12 +00:00
|
|
|
|
public static uint ToUInt32(ReadOnlySpan<byte> data)
|
|
|
|
|
{
|
|
|
|
|
int val = 0;
|
|
|
|
|
val |= data[0] << 24;
|
|
|
|
|
val |= data[1] << 16;
|
|
|
|
|
val |= data[2] << 8;
|
|
|
|
|
val |= data[3];
|
|
|
|
|
return (uint)val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ushort ToUInt16(ReadOnlySpan<byte> data)
|
|
|
|
|
{
|
|
|
|
|
int val = 0;
|
|
|
|
|
val |= data[0] << 8;
|
|
|
|
|
val |= data[1];
|
|
|
|
|
return (ushort)val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int ToInt32(ReadOnlySpan<byte> data)
|
|
|
|
|
{
|
|
|
|
|
int val = 0;
|
|
|
|
|
val |= data[0] << 24;
|
|
|
|
|
val |= data[1] << 16;
|
|
|
|
|
val |= data[2] << 8;
|
|
|
|
|
val |= data[3];
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static short ToInt16(ReadOnlySpan<byte> data)
|
|
|
|
|
{
|
|
|
|
|
int val = 0;
|
|
|
|
|
val |= data[0] << 8;
|
|
|
|
|
val |= data[1];
|
|
|
|
|
return (short)val;
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2021-08-20 20:42:25 +00:00
|
|
|
|
(data[i], data[j]) = (data[j], data[i]);
|
2019-02-15 19:46:02 +00:00
|
|
|
|
i++;
|
|
|
|
|
j--;
|
|
|
|
|
}
|
|
|
|
|
return data;
|
2016-09-26 23:43:52 +00:00
|
|
|
|
}
|
2017-02-08 23:51:18 +00:00
|
|
|
|
|
2020-10-01 05:46:07 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Swaps byte ordering in a byte array based on 32bit value writes.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>The <see cref="data"/> is reversed in-place.</remarks>
|
|
|
|
|
public static void SwapBytes32(byte[] data)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < data.Length; i += 4)
|
|
|
|
|
{
|
2021-08-20 20:42:25 +00:00
|
|
|
|
(data[0 + i], data[3 + i]) = (data[3 + i], data[0 + i]);
|
|
|
|
|
(data[1 + i], data[2 + i]) = (data[2 + i], data[1 + i]);
|
2020-10-01 05:46:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-26 23:43:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|