码迷,mamicode.com
首页 > 其他好文 > 详细

各类型转换成byte[] 和HexString

时间:2018-06-14 14:56:44      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:sys   ++   system   进制   tle   lan   substr   set   etl   

    public class ByteUtil
    {
        /// <summary>
        /// string >>Length
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int getLength(String str)
        {
            return getBytes(str).Length;
        }
        /// <summary>
        /// string >Encoding>byte[]
        /// </summary>
        /// <param name="data"></param>
        /// <param name="charsetName"></param>
        /// <returns></returns>
        public static byte[] getBytes(String data, String charsetName)
        {
            return System.Text.Encoding.GetEncoding(charsetName).GetBytes(data);
        }
        /// <summary>
        /// string >GBK>byte[]
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] getBytes(String data)
        {
            return getBytes(data, "GBK");
        }
        /// <summary>
        /// short>>byte[]
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] getBytes(short data)
        {
            byte[] bytes = new byte[2];
            bytes[0] = (byte)(data & 0xFF);
            bytes[1] = (byte)((data & 0xFF00) >> 8);
            return bytes;
        }
        /// <summary>
        /// char>>byte[定长]
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] getBytes(char c, int count)
        {
            byte[] bytes = new byte[count];
            for (int i = 0; i < bytes.Length; ++i)
            {
                bytes[i] = (byte)c;
            }
            return bytes;
        }
        /// <summary>
        /// int>length==4>byte[]
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] getBytes(int data)
        {
            byte[] bytes = new byte[4];
            bytes[0] = (byte)(data & 0xFF);
            bytes[1] = (byte)((data & 0xFF00) >> 8);
            bytes[2] = (byte)((data & 0xFF0000) >> 16);
            bytes[3] = (byte)((data & 0xFF000000) >> 24);
            return bytes;
        }
        /// <summary>
        /// long>>byte[]
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] getBytes(long data)
        {
            byte[] bytes = new byte[8];
            bytes[0] = (byte)(int)(data & 0xFF);
            bytes[1] = (byte)(int)(data >> 8 & 0xFF);
            bytes[2] = (byte)(int)(data >> 16 & 0xFF);
            bytes[3] = (byte)(int)(data >> 24 & 0xFF);
            bytes[4] = (byte)(int)(data >> 32 & 0xFF);
            bytes[5] = (byte)(int)(data >> 40 & 0xFF);
            bytes[6] = (byte)(int)(data >> 48 & 0xFF);
            bytes[7] = (byte)(int)(data >> 56 & 0xFF);
            return bytes;
        }
        /// <summary>
        /// float>>byte[]
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] getBytes(float data)
        {
            int intBits = BitConverter.ToInt32(BitConverter.GetBytes(data), 0);

            return getBytes(intBits);
        }
        /// <summary>
        /// GB2312 byte【】==》 中文
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string LanChange(byte[] data)
        {
            Encoding utf8;
            Encoding gb2312;
            utf8 = Encoding.GetEncoding("UTF-8");
            gb2312 = Encoding.GetEncoding("GB2312");
            data = Encoding.Convert(gb2312, utf8, data);
            return utf8.GetString(data);
        }
        /// <summary>
        /// 中文==》 GB2312 byte【】
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] ChangeLan(string data)
        {
            byte[] bs = Encoding.GetEncoding("UTF-8").GetBytes(data);
            bs = Encoding.Convert(Encoding.GetEncoding("UTF-8"), Encoding.GetEncoding("GB2312"), bs);
            return bs;
        }
        /// <summary>
        /// 16进制字符串转回string
        /// </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);
                if (strTemp != "\0\0")
                {
                    b[i] = Convert.ToByte(strTemp, 16);
                }
            }
            //按照指定编码将字节数组变为字符串
            return encode.GetString(b);
        }
        /// <summary>
        /// string 转回16进制字符串
        /// </summary>
        /// <param name="hs"></param>
        /// <param name="encode"></param>
        /// <returns></returns>
        public static string StringToHexString(string s, Encoding encode)
        {
            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);
            }
            return result;
        }
    }

各类型转换成byte[] 和HexString

标签:sys   ++   system   进制   tle   lan   substr   set   etl   

原文地址:https://www.cnblogs.com/-NickWang/p/9182315.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!