Compress Int : int « Data Types « C# / C Sharp






Compress Int

        
/*  
 * PERWAPI - An API for Reading and Writing PE Files
 * 
 * Copyright (c) Diane Corney, Queensland University of Technology, 2004.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the PERWAPI Copyright as included with this
 * distribution in the file PERWAPIcopyright.rtf.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY as is explained in the copyright notice.
 *
 * The author may be contacted at d.corney@qut.edu.au
 * 
 * Version Date:  26/01/07
 */

using System;

namespace QUT.PERWAPI
{

    /// <summary>
    /// Diagnostic
    /// </summary>
    public class Diag
    {
        /// <summary>
        /// Flag for diagnostic output.
        /// </summary>
        public static bool DiagOn = false;
    }

    public static class BlobUtil
    {
        public static byte[] CompressInt(int val)
        {
            // This code is based on a revised version of the
            // ECMA-335 spec which clarifies the encoding of 
            // array lower bounds. (kjg 2008-Feb-22 )
            //
            uint uVal = ((uint)val) << 1;
            uint sign = (val < 0 ? (uint)1 : 0);

            if (-64 <= val && val <= 63)
            {
                uVal = uVal & 0x7f | sign;
                return new byte[] { (byte)uVal };
            }
            else if (-8192 <= val && val <= 8191)
            {
                uVal = uVal & 0x3fff | 0x8000 | sign;
                return new byte[] { (byte)(uVal >> 8), (byte)uVal };
            }
            else if (-268435456 <= val && val <= 268435455)
            {
                uVal = uVal & 0x1fffffff | 0xc0000000 | sign;
                return new byte[] { 
                    (byte)(uVal >> 24), 
                    (byte)(uVal >> 16), 
                    (byte)(uVal >> 8), 
                    (byte)uVal };
            }
            else 
                throw new OverflowException("Value cannot be compressed");
        }

        public static byte[] CompressUInt(uint val)
        {
            if (val > 0x1fffffff)
                throw new OverflowException("Value cannot be compressed");
            return CompressNum(val);
        }

        private static byte[] CompressNum(uint num)
        {
            byte[] rslt = null;            
            if (num <= 0x7f)
            {
                rslt = new byte[1]; 
                rslt[0] = (byte)num;
            }
            else if (num <= 0x3fff)
            {
                rslt = new byte[2];
                rslt[0] = (byte)((num >> 8) | 0x80);
                rslt[1] = (byte)(num & 0xff);
            }
            else
            {
                rslt = new byte[4];
                rslt[0] = (byte)((num >> 24) | 0xc0);
                rslt[1] = (byte)((num >> 16) & 0xff);
                rslt[2] = (byte)((num >> 8) & 0xff);
                rslt[3] = (byte)(num & 0xff);
            }
            return rslt;
        }
    }


}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.Assign value to int variable
2.Call methods from primitive data types
3.Calculating the product of three integers.
4.Comparing integers using if statements, equality operators, and relational operators.
5.demonstrates variablesdemonstrates variables
6.Assing value to int valueAssing value to int value
7.Calculate: int and doubleCalculate: int and double
8.Size of int, double, char and boolSize of int, double, char and bool
9.displays a conversion table of Fahrenheit to Celsiusdisplays a conversion  
   table of Fahrenheit to Celsius
10.Some Operator on int
11.Using Integers
12.Using Variables
13.Convert string value to integer by using the int.TryParse
14.Use CultureInfo int ToString method
15.Integer OverFlow
16.Catch OverflowException Exception
17.Do calculation with int variable
18.Format int in Console.WriteLine
19.Use CultureInfo in int.ToString method
20.Use #, % and in int format
21.int based Fahrenheit and Celsius (Centigrade) Scales
22.int array property
23.Parse int value
24.The precision specifier controls the number of significant digits or zeros to the right of a decimal:
25.Int value To Hex char
26.Get the digit-length of an int value
27.Max/Min for int and double values
28.Read sync safe int 32
29.Write Synch safe Int32
30.Calculate average for a list of integer values with params int[] values
31.Int extension Convert integer to boolean, is it even, is it positive
32.Converts an integer into a roman numeral.
33.Get Ordinal
34.Output with x8 format
35.Int32.MaxValue and Int32.MinValue
36.Convert Bytes To UInt
37.Compress UInt
38.Decompress UInt
39.Write Compressed UInt32
40.Read Compressed UInt32
41.Is even