Skip to content

Instantly share code, notes, and snippets.

@mao-test-h
Created December 7, 2019 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mao-test-h/13c86593fc97d379ec563bcf33196fc8 to your computer and use it in GitHub Desktop.
Save mao-test-h/13c86593fc97d379ec563bcf33196fc8 to your computer and use it in GitHub Desktop.
エンディアン変換
// refered to:
// - https://takachan.hatenablog.com/entry/2018/03/10/020555
using System;
namespace Utility
{
internal static class EndianConverter
{
public static char Reverse(char value) => (char)Reverse((ushort)value);
public static short Reverse(short value) => (short)Reverse((ushort)value);
public static int Reverse(int value) => (int)Reverse((uint)value);
public static long Reverse(long value) => (long)Reverse((ulong)value);
public static ushort Reverse(ushort value)
{
return (ushort)((value & 0xFF) << 8 | (value >> 8) & 0xFF);
}
public static uint Reverse(uint value)
{
return (value & 0xFF) << 24 |
((value >> 8) & 0xFF) << 16 |
((value >> 16) & 0xFF) << 8 |
((value >> 24) & 0xFF);
}
public static ulong Reverse(ulong value)
{
return (value & 0xFF) << 56 |
((value >> 8) & 0xFF) << 48 |
((value >> 16) & 0xFF) << 40 |
((value >> 24) & 0xFF) << 32 |
((value >> 32) & 0xFF) << 24 |
((value >> 40) & 0xFF) << 16 |
((value >> 48) & 0xFF) << 8 |
((value >> 56) & 0xFF);
}
public static float Reverse(float value)
{
byte[] bytes = BitConverter.GetBytes(value);
Array.Reverse(bytes);
return BitConverter.ToSingle(bytes, 0);
}
public static double Reverse(double value)
{
byte[] bytes = BitConverter.GetBytes(value);
Array.Reverse(bytes);
return BitConverter.ToDouble(bytes, 0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment