예제 #1
0
        /// <summary>
        /// 인자로 넘어온 값(16진수값)이 음수이면 음수계산 로직을 탄다.
        /// </summary>
        /// <param name="strData"></param>
        /// <returns></returns>
        /// <comment>
        /// 주로 실처리 값에 사용
        /// 양의 수                1의 보수               2의 보수(음의 수표현)
        /// 0000 0000 0000 0001(1) -> 1111 1111 1111 1110 -> 1111 1111 1111 1111(-1)
        /// 0000 0000 0000 0010(2) -> 1111 1111 1111 1101 -> 1111 1111 1111 1110(-2)
        /// 0000 0000 0000 0011(3) -> 1111 1111 1111 1100 -> 1111 1111 1111 1101(-3)
        /// 0000 0000 0000 0100(4) -> 1111 1111 1111 1011 -> 1111 1111 1111 1100(-4)
        /// </comment>
        public static string funPlusMinusAPDCalc(string strData)
        {
            string strBin    = "";
            string dstrTemp  = "";
            string dstrValue = "";
            int    dint      = 0;

            try
            {
                strBin = funHexConvert(strData, EnuEQP.StringType.Binary); //16진수 -> 2진수로 변환

                if (strBin.Substring(0, 1) == "1")                         //최상위비트(MSB)가 1이면 음수값임.
                {
                    //우선 수를 0->1, 1->1로 뒤집는다.
                    for (int dintLoop = 1; dintLoop <= 16; dintLoop++)
                    {
                        if (strBin.Substring(dintLoop - 1, 1) == "0")
                        {
                            dstrTemp = dstrTemp + "1";
                        }
                        else
                        {
                            dstrTemp = dstrTemp + "0";
                        }
                    }

                    strBin = dstrTemp;

                    //위에서 뒤집은 수에다 1을 더해준다.(논리연산)
                    for (int dintLoop = 1; dintLoop <= 16; dintLoop++)
                    {
                        if (strBin.Substring(strBin.Length - dintLoop, 1) == "0")
                        {
                            dstrValue = strBin.Substring(0, 16 - dintLoop) + "1" + FunStringH.funMakeLengthStringFirst("0", dint);
                            break;      //For문을 빠져나간다.
                        }
                        else
                        {
                            dint = dint + 1;
                        }
                    }

                    dstrValue = funBinConvert(dstrValue, EnuEQP.StringType.Decimal);
                    dstrValue = "-" + dstrValue;    //계산된 2진수값을 10진수로 변환후 -를 붙여준다.(음수값이기 때문임)
                }
                else
                {
                    dstrValue = funBinConvert(strBin, EnuEQP.StringType.Decimal);
                }
            }
            catch (Exception ex)
            {
                //this.PInfo.subLog_Set(InfoAct.clsInfo.LogType.CIM, ex.ToString());
                throw (ex);
            }

            return(dstrValue);
        }