Archive for February, 2009

.NET

Decimal to Byte array (vice versa)

14

        /// <summary>
        /// Decimal to Bye array
        /// </summary>
        /// <param name="dec">decimal</param>
        /// <returns>byte array</returns>
        public static byte[] DecimalToBytes(Decimal dec)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(stream))
                {
                    writer.Write(dec);

                    return stream.ToArray();
                }
            }
        }

        /// <summary>
        /// Byte array to Decimal
        /// </summary>
        /// <param name="src">byte array</param>
        /// <returns>decimal</returns>
        public static Decimal BytesToDecimal(byte[] src)
        {
            if (src.Length == 1)
            {
                return Decimal.Parse(((char)src[0]).ToString());
            }

           using (MemoryStream stream = new MemoryStream(src))
           {
               using (BinaryReader reader = new BinaryReader(stream))
               {
                   return reader.ReadDecimal();
               }

           }
        }
.NET

Byte array to String (vice versa)

0

        /// <summary>
        /// Byte arrary to String
        /// </summary>
        /// <param name="bytes">byte array</param>
        /// <returns>string</returns>
        public static string BytesToString(byte[] bytes)
        {
            ASCIIEncoding enc = new System.Text.ASCIIEncoding();

            return enc.GetString(bytes);
        }

        /// <summary>
        /// String to Byte array
        /// </summary>
        /// <param name="str">string</param>
        /// <returns>byte array</returns>
        public static byte[] StringToBytes(string str)
        {
            ASCIIEncoding enc = new System.Text.ASCIIEncoding();

            return enc.GetBytes(str);
        }    
.NET

Byte array to Integer (vice versa)

0

        /// <summary>
        /// Byte array to Integer
        /// </summary>
        /// <param name="bytes">byte array</param>
        /// <returns>int</returns>
        public static int BytesToInt(byte[] bytes)
        {
            return int.Parse(BytesToString(bytes).Trim());
        }

        /// <summary>
        /// Byte array to String
        /// </summary>
        /// <param name="bytes">byte array</param>
        /// <returns>string</returns>
        public static string BytesToString(byte[] bytes)
        {
            ASCIIEncoding enc = new System.Text.ASCIIEncoding();

            return enc.GetString(bytes);
        }

        /// <summary>
        /// Integer to Byte array
        /// </summary>
        /// <param name="value">int</param>
        /// <returns>byte array</returns>
        public static byte[] IntToBytes(int value)
        {
            ASCIIEncoding enc = new System.Text.ASCIIEncoding();

            return enc.GetBytes(value.ToString());

More >

.NET

Hexadecimal to Integer (vice versa)

0

        /// <summary>
        /// Hexa decimal to String
        /// </summary>
        /// <param name="HexString">hexa decimal string</param>
        /// <returns>int</returns>
        public static int HexToInteger(string HexString)
        {
            return int.Parse(HexString, System.Globalization.NumberStyles.HexNumber);
        }

        /// <summary>
        /// Integer to Hexa decimal string
        /// </summary>
        /// <param name="IntValue">int</param>
        /// <returns>hexa decimal string</returns>
        public static string IntegerToHex(int IntValue)
        {
            return IntValue.ToString("X");
        }
.NET

Hexadecimal string to Bytes array

0

        /// <summary>
        /// Hexa decimal to Byte array
        /// </summary>
        /// <param name="HexString">Hexa decimal string</param>
        /// <returns>byte array</returns>
        public static byte[] HexToBytes(String hexString)
        {
            byte[] byteArray = null;
            int byteCount = 0;

            if (hexString != null && hexString.Length > 0)
            {
                // make sure we have an even number of characters
                if (hexString.Length % 2 != 0)
                    hexString = "0" + hexString;

                byteCount = hexString.Length / 2;
                byteArray = new byte[byteCount];
                string sTemp;

                for (int i = 0; i < byteCount; i++)
                {
                    sTemp = hexString.Substring(i * 2, 2);
                    byteArray[i] = Convert.ToByte(sTemp, 16);
                }
            }

            return byteArray;
        }
.NET

Bytes array to Hexadecimal string

0

        /// <summary>
        /// Byte array to Hexa decimal string
        /// </summary>
        /// <param name="bytes">byte array</param>
        /// <returns>hexa decimal string</returns>
        public static string BytesToHex(byte[] bytes)
        {
            string hex = BitConverter.ToString(bytes);

            return hex.Replace("-", "");
        }

        /// <summary>
        /// Byte array to Hexa decimal string
        /// </summary>
        /// <param name="bytes">byte array</param>
        /// <param name="includeHyphen">bool include hyphen</param>
        /// <returns>hexa decimal string</returns>
        public static string BytesToHex(byte[] bytes, bool includeHyphen)
        {
            string hex = BitConverter.ToString(bytes);

            return hex;
        }
SQL Server

Find the balance amount (sum(credit) – sum(debit)) from the following table?

1

Ans: find out the balance amount of account no: 1


select  a.SumCr as Credit ,
        b.SumDr as Debit ,
        a.SumCr - b.SumDr as Balance
from    ( select    sum(A_Amount) as SumCr
          from      tblAccount
          where     A_ID = 1
                    and A_Type = 'Cr'
        ) a ,
        ( select    sum(A_Amount) as SumDr
          from      tblAccount
          where     A_ID = 1
                    and A_Type = 'Dr'
        ) b

Ans: find out the balance amount for all the account numbers in the More >

SQL Server

Find out the N’th maxium salary?

1

Ans: find the 5th maxium salary:

   1: SELECT TOP 1 * FROM tblEmployee WHERE E_Salary NOT IN 
   2:     (SELECT DISTINCT TOP 4 E_Salary FROM tblEmployee ORDER BY E_Salary DESC)
   3:     ORDER BY E_Salary DESC

.NET

Auto Complete ComboBox – VB.NET Windows Forms

3

Introduction

This is a simple code snippet which is used to make an Auto Complete ComboBox.

Using the code

Usage: Call the subroutine from within the ComboBox’s KeyPress event handler.


AutoComplete(ByRef cb As ComboBox, ByVal e As System.Windows.Forms.KeyPressEventArgs, Optional ByVal blnLimitToList As Boolean = False)
  • cb – ComboBox used to make the Auto Complete ComboBox.

More >

.NET

Auto Complete ComboBox – C# Windows Forms

3

Introduction

This is a simple code snippet which is used to make an Auto Complete ComboBox.

Using the code

Usage: Call the function AutoComplete from within the ComboBox’s KeyPress event handler.


AutoComplete(ComboBox cb, System.Windows.Forms.KeyPressEventArgs e, bool blnLimitToList)

Code


        /// <summary>
        /// AutoComplete
        /// </summary>
        /// <param name="cb">ComboBox</param>
        /// <param name="e">KeyPressEventArgs</param>

More >

Go to Top