sharp-extensions/UInt64Extension.cs

99 lines
2.6 KiB
C#

using System;
using System.Runtime.CompilerServices;
namespace sharp.extensions
{
public static class UInt64Extender
{
public static UInt64 RotateLeft(this UInt64 value, int count)
{
return (value << count) | (value >> (64 - count));
}
public static UInt64 RotateRight(this UInt64 value, int count)
{
return (value >> count) | (value << (64 - count));
}
public static byte[] GetBytes(this UInt64 value){
return BitConverter.GetBytes(value);
}
public static byte[] GetBytes(this UInt64 value,Endianess endianess)
{
byte[] r = BitConverter.GetBytes(value);
if (endianess != value.CurrentEndianess()){
Array.Reverse(r);
}
return r;
}
public static byte[] GetBytes(this Int64 value)
{
return BitConverter.GetBytes(value);
}
public static byte[] GetBytes(this Int64 value, Endianess endianess)
{
byte[] r = BitConverter.GetBytes(value);
if (endianess != value.CurrentEndianess())
{
Array.Reverse(r);
}
return r;
}
public static byte[] GetBytes(this Int32 value)
{
return BitConverter.GetBytes(value);
}
public static byte[] GetBytes(this Int32 value, Endianess endianess)
{
byte[] r = BitConverter.GetBytes(value);
if (endianess != value.CurrentEndianess())
{
Array.Reverse(r);
}
return r;
}
public static byte[] GetBytes(this UInt32 value)
{
return BitConverter.GetBytes(value);
}
public static byte[] GetBytes(this UInt32 value, Endianess endianess)
{
byte[] r = BitConverter.GetBytes(value);
if (endianess != value.CurrentEndianess())
{
Array.Reverse(r);
}
return r;
}
public static Endianess CurrentEndianess(this UInt64 v) { return BitConverter.IsLittleEndian ? Endianess.LittleEndian : Endianess.BigEndian; }
public static Endianess CurrentEndianess(this UInt32 v) { return BitConverter.IsLittleEndian ? Endianess.LittleEndian : Endianess.BigEndian; }
public static Endianess CurrentEndianess(this Int64 v) { return BitConverter.IsLittleEndian ? Endianess.LittleEndian : Endianess.BigEndian; }
public static Endianess CurrentEndianess(this Int32 v) { return BitConverter.IsLittleEndian ? Endianess.LittleEndian : Endianess.BigEndian; }
public static byte[] getBytes(this UInt32[] values)
{
byte[] b = new byte[values.Length << 2];
for (int n = 0; n < values.Length; n++)
{
b.Insert(values[n].GetBytes(), n << 2);
}
return b;
}
public static UInt32[] toUInt32(this byte[] values)
{
UInt32[] r = new UInt32[ (values.Length + 0x03) >> 2 ];
values = values.Extend(r.Length << 2);
for (int n = 0; n < r.Length; n++)
{
r[n] = BitConverter.ToUInt32(values, (n << 2));
}
return r;
}
}
}