ConverHelper.cs 5.62 KB
using System;
using System.Text;

namespace HHECS.Infrastructure.CommonHelper
{
    /// <summary>
    /// 数据类型转换帮助类
    /// </summary>
    public static class ConverHelper
    {
        #region 字符串和Byte之间的转化

        /// <summary>
        /// 将字符串转为16进制字符,允许中文
        /// </summary>
        /// <param name="s"></param>
        /// <param name="encode"></param>
        /// <returns></returns>
        public static string StringToHexString(string s, Encoding encode, string spanString)
        {
            byte[] b = encode.GetBytes(s);//按照指定编码将string编程字节数组
            string result = string.Empty;
            for (int i = 0; i < b.Length; i++)//逐字节变为16进制字符
            {
                result += Convert.ToString(b[i], 16) + spanString;
            }
            return result;
        }

        /// <summary>
        /// 将16进制字符串转为字符串
        /// </summary>
        /// <param name="hs"></param>
        /// <param name="encode"></param>
        /// <returns></returns>
        public static string HexStringToString(string hs, Encoding encode)
        {
            string strTemp = "";
            byte[] b = new byte[hs.Length / 2];
            for (int i = 0; i < hs.Length / 2; i++)
            {
                strTemp = hs.Substring(i * 2, 2);
                b[i] = Convert.ToByte(strTemp, 16);
            }
            //按照指定编码将字节数组变为字符串
            return encode.GetString(b);
        }

        /// <summary>
        /// byte[]转为16进制字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string ByteToHexStr(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2");
                }
            }
            return returnStr;
        }

        /// <summary>
        /// 将16进制的字符串转为byte[]
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static byte[] StrToHexByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }

        /// <summary>
        /// ASCII码转字符
        /// </summary>
        /// <param name="ints"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public static string ASCIIToString(short[] ints)
        {
            String str = "";
            ASCIIEncoding asciiEncoding = new ASCIIEncoding();
            foreach (var item in ints)
            {
                if (item >= 0 && item <= 255)
                {

                    byte[] byteArray = new byte[] { (byte)item };
                    string strCharacter = asciiEncoding.GetString(byteArray);
                    str += strCharacter;
                }
                else
                {
                    throw new ArgumentException("ASCII Code is not valid.");
                }
            }
            return str;
        }

        /// <summary>
        /// 字符转ASCII,这里默认20位,不够填充空格
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static byte[] StringToASCII(string str)
        {
            ASCIIEncoding asciiEncoding = new ASCIIEncoding();
            return asciiEncoding.GetBytes(str.PadRight(20, ' '));
        }

        public static short Asc(string character)
        {
            if (character.Length == 1)
            {
                ASCIIEncoding asciiEncoding = new ASCIIEncoding();
                short intAsciiCode = (short)asciiEncoding.GetBytes(character)[0];
                return (intAsciiCode);
            }
            else
            {
                throw new Exception("Character is not valid.");
            }

        }


        #endregion

        #region 字节位操作

        /// <summary>
        /// 对于一个字节b,从0开始,获取左起第index位,比如第0位,第2位
        /// </summary>
        /// <param name="b"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static int GetBit(byte b, int index) { return ((b & (1 << index)) > 0) ? 1 : 0; }

        /// <summary>
        /// 从0位起始,设置低index位为1
        /// </summary>
        /// <param name="b"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static byte SetBit(byte b, int index) { return (byte)(b | (1 << index)); }

        /// <summary>
        /// 从0位起始,设置第index位为0
        /// </summary>
        /// <param name="b"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static byte ClearBit(byte b, int index) { return (byte)(b & (byte.MaxValue - (1 << index))); }

        /// <summary>
        /// 从0为开始,将第index位取反
        /// </summary>
        /// <param name="b"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static byte ReverseBit(byte b, int index) { return (byte)(b ^ (byte)(1 << index)); }

        #endregion
    }
}