UniVRM/UniJSON/Scripts/Extensions/ByteExtensions.cs
ousttrue df85433628 Merge commit 'df5f79584312cf4f6cc7ec1d73a78f6ba475c9ad' as 'UniJSON'
Co-authored-by: Deatrathias <dmailsec@gmail.com>
Co-authored-by: dj-kusuha <dj.kusuha+github@gmail.com>
Co-authored-by: ousttrue <ousttrue@gmail.com>
Co-authored-by: yutopp <yutopp@gmail.com>
2018-12-28 20:38:39 +09:00

61 lines
1.8 KiB
C#

using System;
using System.IO;
namespace UniJSON
{
public static class ByteExtensions
{
public static Byte GetHexDigit(this UInt16 n, int index)
{
return (Byte)(n >> 8 * index & 0xff);
}
public static Byte GetHexDigit(this UInt32 n, int index)
{
return (Byte)(n >> 8 * index & 0xff);
}
public static Byte GetHexDigit(this UInt64 n, int index)
{
return (Byte)(n >> 8 * index & 0xff);
}
public static Byte GetHexDigit(this Int16 n, int index)
{
return (Byte)(n >> 8 * index & 0xff);
}
public static Byte GetHexDigit(this Int32 n, int index)
{
return (Byte)(n >> 8 * index & 0xff);
}
public static Byte GetHexDigit(this Int64 n, int index)
{
return (Byte)(n >> 8 * index & 0xff);
}
public static UInt32 ToUint32(this Single n, Byte[] buffer)
{
if (buffer.Length < 4)
{
throw new ArgumentException();
}
using (var ms = new MemoryStream(buffer))
using (var w = new BinaryWriter(ms))
{
w.Write(n);
}
return BitConverter.ToUInt32(buffer, 0);
}
public static UInt64 ToUint64(this Double n, Byte[] buffer)
{
if (buffer.Length < 8)
{
throw new ArgumentException();
}
using (var ms = new MemoryStream(buffer))
using (var w = new BinaryWriter(ms))
{
w.Write(n);
}
return BitConverter.ToUInt64(buffer, 0);
}
}
}