Mega Code Archive

 
Categories / C# / Data Types
 

Compress UInt

using System; using System.Collections.Generic; using System.Text; using System.Reflection; public static class Utility {     public static byte[] CompressUInt(uint data)     {         if (data <= 0x7F)         {             var bytes = new byte[1];             bytes[0] = (byte)data;             return bytes;         }         else if (data <= 0x3FFF)         {             var bytes = new byte[2];             bytes[0] = (byte)(((data & 0xFF00) >> 8) | 0x80);             bytes[1] = (byte)(data & 0x00FF);             return bytes;         }         else if (data <= 0x1FFFFFFF)         {             var bytes = new byte[4];             bytes[0] = (byte)(((data & 0xFF000000) >> 24) | 0xC0);             bytes[1] = (byte)((data & 0x00FF0000) >> 16);             bytes[2] = (byte)((data & 0x0000FF00) >> 8);             bytes[3] = (byte)(data & 0x000000FF);             return bytes;         }         else             throw new NotSupportedException();     } }