Get hash code for a byte array

image_pdfimage_print
   
 
//http://www.bouncycastle.org/
//MIT X11 License


using System;
using System.Text;

namespace Org.BouncyCastle.Utilities
{

    /// <summary> General array utilities.</summary>
    public sealed class Arrays
    {
    public static int GetHashCode(byte[] data)
    {
      if (data == null)
      {
        return 0;
      }

      int i = data.Length;
      int hc = i + 1;

      while (--i >= 0)
      {
        hc *= 257;
        hc ^= data[i];
      }

      return hc;
    }
    }
}

   
     


Binary Network Byte Order

image_pdfimage_print

   
  
using System;
using System.Net;
using System.Text;

public class BinaryNetworkByteOrder
{
   public static void Main()
   {
      short test1 = 45;
      int test2 = 314159;
      long test3 = -123456789033452;
      byte[] data = new byte[1024];
      string output;

      data = BitConverter.GetBytes(test1);
      output = BitConverter.ToString(data);
      Console.WriteLine("test1 = {0}, string = {1}", test1, output);

      data = BitConverter.GetBytes(test2);
      output = BitConverter.ToString(data);
      Console.WriteLine("test2 = {0}, string = {1}", test2, output);

      data = BitConverter.GetBytes(test3);
      output = BitConverter.ToString(data);
      Console.WriteLine("test3 = {0}, string = {1}", test3, output);

      short test1b = IPAddress.HostToNetworkOrder(test1);
      data = BitConverter.GetBytes(test1b);
      output = BitConverter.ToString(data);
      Console.WriteLine("test1 = {0}, nbo = {1}", test1b, output);

      int test2b = IPAddress.HostToNetworkOrder(test2);
      data = BitConverter.GetBytes(test2b);
      output = BitConverter.ToString(data);
      Console.WriteLine("test2 = {0}, nbo = {1}", test2b, output);

      long test3b = IPAddress.HostToNetworkOrder(test3);
      data = BitConverter.GetBytes(test3b);
      output = BitConverter.ToString(data);
      Console.WriteLine("test3 = {0}, nbo = {1}", test3b, output);
   }
}

           
         
    
     


Binary Data Test

image_pdfimage_print

   
  
using System;
using System.Net;
using System.Text;

public class BinaryDataTest
{
   public static void Main()
   {
      int test1 = 45;
      double test2 = 3.14159;
      int test3 = -1234567890;
      bool test4 = false;
      byte[] data = new byte[1024];
      string output;

      data = BitConverter.GetBytes(test1);
      output = BitConverter.ToString(data);
      Console.WriteLine("test1 = {0}, string = {1}", test1, output);

      data = BitConverter.GetBytes(test2);
      output = BitConverter.ToString(data);
      Console.WriteLine("test2 = {0}, string = {1}", test2, output);

      data = BitConverter.GetBytes(test3);
      output = BitConverter.ToString(data);
      Console.WriteLine("test3 = {0}, string = {1}", test3, output);

      data = BitConverter.GetBytes(test4);
      output = BitConverter.ToString(data);
      Console.WriteLine("test4 = {0}, string = {1}", test4, output);
   }
}

           
         
    
     


Obtaining the Most Significant or Least Significant Bits of a Number

image_pdfimage_print

   
  

using System;
using System.Data;


class Class1{
        static void Main(string[] args){
            int number = 25;
            short num = 25;
            Console.WriteLine(GetMSB(number));
            Console.WriteLine(GetConvertMSB(number));
            Console.WriteLine(GetLSB(number));
            Console.WriteLine(GetConvertLSB(number));
            Console.WriteLine(GetMSB(num));
            Console.WriteLine(GetLSB(num));

        }
        public static int GetMSB(int value)
        {
            return (int)(value &amp; 0xFFFF0000);
        }
        public static int GetConvertMSB(int value)
        {
            return (value &amp; Convert.ToInt32("11111111111111110000000000000000", 2));
        }

        public static int GetLSB(int intValue)
        {
            return (intValue &amp; 0x0000FFFF);
        }

        public static int GetConvertLSB(int intValue)
        {
            return (intValue &amp; Convert.ToInt32("11111111111111110000000000000000", 2));
        }
        
        public static int GetMSB(short intValue)
        {
            return (intValue &amp; 0xFF00);
        }

        public static int GetLSB(short intValue)
        {
            return (intValue &amp; 0x00FF);
        }

}

           
         
    
     


Using the Bitwise Complement Operators with Various Data Types

image_pdfimage_print

   
  

using System;
using System.Data;


class Class1{
        static void Main(string[] args){
      uint x = 0x01001001;
      uint XComp = ~x;
      Console.WriteLine("~x = " + ~x);

      sbyte B1 = sbyte.MinValue;  
      sbyte B2 = sbyte.MaxValue;
      Console.WriteLine("B1|B2 = " + (((byte)B1|(byte)B2)));

      ushort x2 = 0x00000001;           // Problem
      Console.WriteLine("~x2 = " + ~x2);

      byte y = 1;                       // Problem
      //byte B = ~y;
      Console.WriteLine("~y = " + ~y);

      char x3 = (char)1;               // Problem
      Console.WriteLine("~x3 = " + ~x3);

      sbyte x5 = 1;
      Console.WriteLine("~x5 = " + ~x5);
      
      uint IntResult = (uint)~x;
      Console.WriteLine("IntResult = " + IntResult);
      
      byte ByteResult = (byte)~y;
      Console.WriteLine("ByteResult = " + ByteResult);
        }

}

           
         
    
     


.NET Frameworks Overview:Custom Object Formatting

image_pdfimage_print

   


using System;
class Employee: IFormattable
{
    public Employee(int id, string firstName, string lastName)
    {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }
    public string ToString (string format, IFormatProvider fp) 
    {
        if ((format != null) &amp;&amp; (format.Equals("F")))
        return(String.Format("{0}: {1}, {2}", 
        id, lastName, firstName));
        else
        return(id.ToString(format, fp));
    }
    int    id;
    string    firstName;
    string    lastName;
}
public class CustomObjectFormatting
{
    public static void Main()
    {
        Employee fred = new Employee(123, "AAA", "BBB");
        Console.WriteLine("No format: {0}", fred);
        Console.WriteLine("Full format: {0:F}", fred);
    }
}