zoukankan      html  css  js  c++  java
  • C#字符串和byte之间的互相转化

    平时总结的字符串和byte的转化操作:

            #region 字符串和Byte之间的转化
            /// <summary>
            /// 数字和字节之间互转
            /// </summary>
            /// <param name="num"></param>
            /// <returns></returns>
            public static int IntToBitConverter(int num)
            {
                int temp = 0;
                byte[] bytes = BitConverter.GetBytes(num);//将int32转换为字节数组
                temp = BitConverter.ToInt32(bytes, 0);//将字节数组内容再转成int32类型
                return temp;
            }
    
            /// <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;
            }
            
            #endregion    
  • 相关阅读:
    [程序员代码面试指南]数组和矩阵问题-未排序正数数组中累加和为给定值的最长子数组长度
    [Mysql]知识点
    [SSM项目]一-Eclipse 搭建marven-web项目 hello world!
    [BZOJ2252]矩阵距离(BFS)
    [Spring实战笔记]4面向切面编程的Spring-代理
    [程序员代码面试指南]数组和矩阵问题-找到无序数组中最小的k个数(堆排序)
    [Java]刷题中的Java基础
    MySql的大小写问题
    MySql密码丢失
    MySql的rpm安装
  • 原文地址:https://www.cnblogs.com/liangxiaoking/p/5958456.html
Copyright © 2011-2022 走看看