Skip to content

Instantly share code, notes, and snippets.

@mkropat
Created January 21, 2018 22:17
Show Gist options
  • Save mkropat/b36fae858b3237d465e67edb55f41ccb to your computer and use it in GitHub Desktop.
Save mkropat/b36fae858b3237d465e67edb55f41ccb to your computer and use it in GitHub Desktop.
<Query Kind="Program" />
void Main()
{
Console.WriteLine($" {"value"} {"short",cs} {"ushort->short",cs} {"short->int",cs} {"ushort",cs} {"short->ushort",cs} {"ushort->int",cs} notes");
PrintShort(new byte[] { 0x00, 0x00 });
PrintShort(new byte[] { 0x00, 0x01 });
PrintShort(new byte[] { 0x7f, 0xfe });
PrintShort(new byte[] { 0x7f, 0xff });
PrintShort(new byte[] { 0x80, 0x00 });
PrintShort(new byte[] { 0x80, 0x01 });
PrintShort(new byte[] { 0xff, 0xfe });
PrintShort(new byte[] { 0xff, 0xff });
// value short ushort->short short->int ushort short->ushort ushort->int notes
// 0x0000 0 0 0 0 0 0 ushort.MinValue
// 0x0001 1 1 1 1 1 1
// 0x7FFE 32766 32766 32766 32766 32766 32766
// 0x7FFF 32767 32767 32767 32767 32767 32767 short.MaxValue
// 0x8000 -32768 -32768 -32768 32768 32768 32768 short.MinValue
// 0x8001 -32767 -32767 -32767 32769 32769 32769
// 0xFFFE -2 -2 -2 65534 65534 65534
// 0xFFFF -1 -1 -1 65535 65535 65535 ushort.MaxValue
}
const int cs = 14;
void PrintShort(byte[] val)
{
if (BitConverter.IsLittleEndian)
Array.Reverse(val);
var notes = new List<string>();
var int16 = BitConverter.ToInt16(val, 0);
if (int16 == short.MinValue)
notes.Add("short.MinValue");
if (int16 == short.MaxValue)
notes.Add("short.MaxValue");
var uint16 = BitConverter.ToUInt16(val, 0);
if (uint16 == ushort.MinValue)
notes.Add("ushort.MinValue");
if (uint16 == ushort.MaxValue)
notes.Add("ushort.MaxValue");
Console.WriteLine($"0x{uint16:X4} {int16,cs} {(short)uint16,cs} {(int)int16,cs} {uint16,cs} {(ushort)int16,cs} {(int)uint16,cs} {string.Join(", ", notes)}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment