为了自由,幸福而不断奋斗,前行!!!

一笑看风云过....

博客园 首页 新随笔 联系 订阅 管理

        /// <summary>
        /// 特列UInt32类型值转换成指定的格式
        /// </summary>
        /// <param name="formatStyle">
        /// 格式参数:
        /// yyyy表示日期的年(4位);yy表示日期的年(2位);
        /// m表示日期的月(自动);mm表示日期的月(2位);
        /// d表示日期的天(自动);dd表示日期的天(2位);
        /// p1表示IP地址的第一部分;p2表示IP地址的第二部分;p3表示IP地址的第三部分;p4表示IP地址的第四部分;
        /// v1表示版本号的第一部分;v2表示版本号的第2部分;[版本号格式:主版本号.次版本号]
        /// vs1表示版本号的第一部分;vs2表示版本号的第2部分;vs3表示版本号的第3部分;vs4表示版本号的第4部分;[版本号格式:x.x.x.x]
        /// </param>
        /// <returns></returns>
        public static string UInt32ToString(UInt32 intValue, string formatStyle)
        {
            //转换为16进制表示值      
            string valueString = intValue.ToString("X");
            string strReturn = formatStyle.ToUpper();
            //格式化
            string strTemp = "00000000";
            strTemp = strTemp.Remove(0, valueString.Length) + valueString;
            string strPart1 = strTemp.Substring(0, 2);
            string strPart2 = strTemp.Substring(2, 2);
            string strPart3 = strTemp.Substring(4, 2);
            string strPart4 = strTemp.Substring(6, 2);
            //日期年的处理
            if (strReturn.IndexOf("YYYY") != -1)
            {
                strReturn = strReturn.Replace("YYYY", int.Parse(strPart1 + strPart2, System.Globalization.NumberStyles.AllowHexSpecifier).ToString());
            }
            if (strReturn.IndexOf("YY") != -1)
            {
                strReturn = strReturn.Replace("YY", (int.Parse(strPart1 + strPart2, System.Globalization.NumberStyles.AllowHexSpecifier) % 100).ToString());
            }
            //日期月的处理
            if (strReturn.IndexOf("MM") != -1)
            {
                int mm = int.Parse(strPart3, System.Globalization.NumberStyles.AllowHexSpecifier);
                strReturn = strReturn.Replace("MM", ((mm > 10 ? mm.ToString() : "0" + mm.ToString())));
            }
            if (strReturn.IndexOf("M") != -1)
            {
                strReturn = strReturn.Replace("M", int.Parse(strPart3, System.Globalization.NumberStyles.AllowHexSpecifier).ToString());
            }
            //日期日的处理
            if (strReturn.IndexOf("DD") != -1)
            {
                int dd = int.Parse(strPart4, System.Globalization.NumberStyles.AllowHexSpecifier);
                strReturn = strReturn.Replace("DD", ((dd > 10 ? dd.ToString() : "0" + dd.ToString())));
            }
            if (strReturn.IndexOf("D") != -1)
            {
                strReturn = strReturn.Replace("D", int.Parse(strPart4, System.Globalization.NumberStyles.AllowHexSpecifier).ToString());
            }
            //IP地址的处理
            if (strReturn.IndexOf("P1") != -1)
            {
                strReturn = strReturn.Replace("P1", int.Parse(strPart1, System.Globalization.NumberStyles.AllowHexSpecifier).ToString());
            }
            if (strReturn.IndexOf("P2") != -1)
            {
                strReturn = strReturn.Replace("P2", int.Parse(strPart2, System.Globalization.NumberStyles.AllowHexSpecifier).ToString());
            }
            if (strReturn.IndexOf("P3") != -1)
            {
                strReturn = strReturn.Replace("P3", int.Parse(strPart1, System.Globalization.NumberStyles.AllowHexSpecifier).ToString());
            }
            if (strReturn.IndexOf("P4") != -1)
            {
                strReturn = strReturn.Replace("P4", int.Parse(strPart1, System.Globalization.NumberStyles.AllowHexSpecifier).ToString());
            }
            //版本号的处理[主版本号.次版本号]
            if (strReturn.IndexOf("V1") != -1)
            {
                strReturn = strReturn.Replace("V1", int.Parse(strPart1 + strPart2, System.Globalization.NumberStyles.AllowHexSpecifier).ToString());
            }
            if (strReturn.IndexOf("V2") != -1)
            {
                strReturn = strReturn.Replace("V2", int.Parse(strPart3 + strPart4, System.Globalization.NumberStyles.AllowHexSpecifier).ToString());
            }
            //版本号的处理[X.X.X.X]
            if (strReturn.IndexOf("VS1") != -1)
            {
                strReturn = strReturn.Replace("VS1", int.Parse(strPart1, System.Globalization.NumberStyles.AllowHexSpecifier).ToString());
            }
            if (strReturn.IndexOf("VS2") != -1)
            {
                strReturn = strReturn.Replace("VS2", int.Parse(strPart2, System.Globalization.NumberStyles.AllowHexSpecifier).ToString());
            }
            if (strReturn.IndexOf("VS3") != -1)
            {
                strReturn = strReturn.Replace("VS3", int.Parse(strPart1, System.Globalization.NumberStyles.AllowHexSpecifier).ToString());
            }
            if (strReturn.IndexOf("VS4") != -1)
            {
                strReturn = strReturn.Replace("VS4", int.Parse(strPart1, System.Globalization.NumberStyles.AllowHexSpecifier).ToString());
            }
            return strReturn;
        }

        /// <summary>
        /// 字符数组转换为字符串
        /// </summary>
        /// <param name="charValue">字符数组</param>
        /// <returns>字符串</returns>
        public static string ByteArrayToString(byte[] byteArray)
        {
            //string result = Encoding.GetEncoding("gb2312").GetString(byteArray);
            //string result = Encoding.GetEncoding(936).GetString(byteArray);
            string result = pEncoding.GetString(byteArray);
            return result;
        }

        /// <summary>
        /// 字符数组转换为字符串[仅适用于字符数据长度为16的字符数组对象]
        /// </summary>
        /// <param name="charValue">字符数组</param>
        /// <param name="formatStyle">字符串格式[不区分大小写] IP1:IP地址的第一部分;IP2:IP地址第二部分;IP3:IP地址的第三部分;IP4:IP地址的第四部分</param>
        /// <returns>字符串</returns>
        public static string ByteArrayToString(byte[] byteArray, string formatStyle)
        {
            //string result = Encoding.GetEncoding("gb2312").GetString(byteArray);
            //string result = Encoding.GetEncoding(936).GetString(byteArray);
            string result = pEncoding.GetString(byteArray);
            if (result.Length == 16)
            {
                string sPart1 = result.Substring(0, 4);
                string sPart2 = result.Substring(4, 4);
                string sPart3 = result.Substring(8, 4);
                string sPart4 = result.Substring(12, 4);
                string strTemp = formatStyle.ToUpper();
                //IP地址格式处理
                if (strTemp.IndexOf("IP1") != -1)
                {
                    strTemp = strTemp.Replace("IP1", int.Parse(sPart1).ToString());
                }
                if (strTemp.IndexOf("IP2") != -1)
                {
                    strTemp = strTemp.Replace("IP2", int.Parse(sPart2).ToString());
                }
                if (strTemp.IndexOf("IP3") != -1)
                {
                    strTemp = strTemp.Replace("IP3", int.Parse(sPart3).ToString());
                }
                if (strTemp.IndexOf("IP4") != -1)
                {
                    strTemp = strTemp.Replace("IP4", int.Parse(sPart4).ToString());
                }
                result = strTemp;
            }
            return result;
        }


        /// <summary>
        /// 字符串转换为字符数组
        /// </summary>
        /// <param name="strValue"></param>
        /// <returns></returns>
        public static bool StringToByteArry(string strValue, ref byte[] byteArry)
        {
            try
            {
                //byte[] byteTemp = Encoding.GetEncoding("gb2312").GetBytes(strValue);
                byte[] byteTemp = pEncoding.GetBytes(strValue);
                int maxLen = (byteTemp.Length > byteArry.Length ? byteArry.Length : byteTemp.Length);
                for (int i = 0; i < byteArry.Length; i++)
                {
                    if (i < maxLen)
                    {
                        byteArry[i] = byteTemp[i];
                    }
                    else
                    {
                        byteArry[i] = new byte();
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

posted on 2008-07-22 15:03  YAO'STAR  阅读(296)  评论(0编辑  收藏  举报