zoukankan      html  css  js  c++  java
  • 号码字符串与BCD编码互转 c#

    /// <summary>
            /// 把号码用BCD进行压缩编码。
            /// </summary>
            /// <param name="Num8BitByte">The num8 bit byte.</param>
            /// <returns></returns>
            public static byte[] ByteArrayToBCD(byte[] Num8BitByte)//8位的ascii码
            {
                byte[] Num4bitByte = new byte[8];

                Num4bitByte = BitConverter.GetBytes(0xffffffffffffffff);
                for (int i = 0; i<Num8BitByte.Length; i++)
                {
                    byte num =Convert.ToByte(Num8BitByte[i] - 0x30);
                    if (i % 2 == 0)
                    {
                        Num4bitByte[i / 2] = Convert.ToByte((Num4bitByte[i / 2] & 0xF0) | num);
                    }
                    else
                    {
                        Num4bitByte[i / 2] = Convert.ToByte((Num4bitByte[i / 2] &0x0F)| (num << 4));
                    }
                      
                }
                
                return Num4bitByte;
            }

            /// <summary>
            /// BCDs to string.
            /// </summary>
            /// <param name="bcdNum">The BCD num.</param>
            /// <returns></returns>
            public static string bcdToString(byte[] bcdNum)
            {
                string retString="";
                byte[] byteChar = new byte[bcdNum.Length];
                byteChar = BitConverter.GetBytes(0xffffffffffffffff);
                byte tempHigh=0,tempLow=0;
                int i = 0;
                while (tempHigh != 0x0F && tempLow != 0xF0)
                {
                    tempHigh = Convert.ToByte(bcdNum[i] & 0xF0);//取出高四位;
                    tempHigh = Convert.ToByte(tempHigh >> 4);
                    tempLow = Convert.ToByte((bcdNum[i] & 0x0F) << 4);
                    byteChar[i] = Convert.ToByte(tempLow | tempHigh);
                    i++;
                } 
                string[] HexString=BitConverter.ToString(byteChar).Trim().Split('-');
                foreach (string str in HexString)
                {
                    retString += str.Trim();
                }
                int LastIndex = retString.IndexOf('F');
                return retString = retString.Substring(0, LastIndex);
            }

  • 相关阅读:
    中国科学院2021年硕转博考试分析试题参考解答
    蒲和平大学生数学竞赛教程答案5.1.3
    清华大学2021年数学推荐免试试题参考解答
    蒲和平大学生数学竞赛教程答案4.1.1
    兰州大学历年数学分析高等代数考研试题答案
    复旦大学2021年数学英才实验班选拔考试试题参考解答pdf
    北京大学2021年基础学科招生考试数学试题
    南开大学2021年数学伯苓班/复旦大学2021年数学英才实验班选拔考试试题
    实变函数与泛函分析第05次课:至1.5.2(请点阅读全文进课堂)
    中国科学技术大学2021年新生入学考试试题
  • 原文地址:https://www.cnblogs.com/94cool/p/3584157.html
Copyright © 2011-2022 走看看