Archive for February 26, 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());
        }

        /// <summary>
        /// Bytes to Integer
        /// </summary>
        /// <param name=”src”></param>

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;
        }
Go to Top