Beispiel #1
0
        private string changeBitnessHelper(string bin, string dec, int MAX, CalcViews.Programmer.ProgrammerCalcBits currentBits, CalcViews.Programmer.ProgrammerCalcBits prevBits)
        {
            string retVal = string.Empty;

            // First we'll resize the bin string to have the right number of bits
            // per the prevBits value
            int prevMax = (prevBits == CalcViews.Programmer.ProgrammerCalcBits.Qword) ? LONG :
                          (prevBits == CalcViews.Programmer.ProgrammerCalcBits.Dword) ? INT :
                          (prevBits == CalcViews.Programmer.ProgrammerCalcBits.Word) ? SHORT : BYTE;

            int count = prevMax - bin.Length;

            for (int i = 0; i < count; i++)
            {
                bin = "0" + bin;
            }

            if (bin.StartsWith("1"))
            {
                count = MAX - bin.Length;
                string tempExp = string.Empty;

                for (int i = 0; i < count; i++)
                {
                    tempExp += "1";
                }

                return(CalcHelpers.bin2Dec(tempExp + bin, (currentView as ProgrammerView).currentBits));
            }

            return(dec);
        }
Beispiel #2
0
        public string GetDecimal(List <int> bin)
        {
            if (!_mainPage.isProgrammer())
            {
                return(string.Empty);
            }

            string exp = string.Empty;

            foreach (int b in bin)
            {
                exp += b.ToString();
            }

            return(CalcHelpers.bin2Dec(exp, (currentView as ProgrammerView).currentBits));
        }
Beispiel #3
0
        private string GetDecimal(string exp, bool asLong)
        {
            string dec = exp;

            if (!_mainPage.isProgrammer())
            {
                return(dec);
            }

            if (asLong)
            {
                if ((currentView as ProgrammerView).calcState == Calc_MainPage.CalcStateValues.Hex)
                {
                    dec = CalcViews.Programmer.hex2Long(exp);
                }
                else if ((currentView as ProgrammerView).calcState == Calc_MainPage.CalcStateValues.Oct)
                {
                    dec = CalcViews.Programmer.oct2Long(exp);
                }
                else if ((currentView as ProgrammerView).calcState == Calc_MainPage.CalcStateValues.Bin)
                {
                    dec = CalcViews.Programmer.bin2Long(exp);
                }
            }

            else
            {
                if ((currentView as ProgrammerView).calcState == Calc_MainPage.CalcStateValues.Hex)
                {
                    dec = CalcHelpers.hex2Dec(exp, (currentView as ProgrammerView).currentBits);
                }
                else if ((currentView as ProgrammerView).calcState == Calc_MainPage.CalcStateValues.Oct)
                {
                    dec = CalcHelpers.oct2Dec(exp, (currentView as ProgrammerView).currentBits);
                }
                else if ((currentView as ProgrammerView).calcState == Calc_MainPage.CalcStateValues.Bin)
                {
                    dec = CalcHelpers.bin2Dec(exp, (currentView as ProgrammerView).currentBits);
                }
            }

            return(dec);
        }
Beispiel #4
0
        public void ChangeBitness(CalcViews.Programmer.ProgrammerCalcBits prevBits)
        {
            if (!_mainPage.isProgrammer())
            {
                _mainPage.initCalcError();
                return;
            }

            string dec = Get();
            string bin = CalcViews.Programmer.dec2Long(dec, 2);

            // next if the user has chosen a specific bitness
            if ((currentView as ProgrammerView).currentBits == CalcViews.Programmer.ProgrammerCalcBits.Qword)
            {
                // ASSUME we're comming from a lower order bit
                dec = changeBitnessHelper(bin, dec, LONG, (currentView as ProgrammerView).currentBits, prevBits);
            }
            else if ((currentView as ProgrammerView).currentBits == CalcViews.Programmer.ProgrammerCalcBits.Dword)
            {
                // if we're going down in bitness
                if (bin.Length > INT)
                {
                    bin = bin.Substring(bin.Length - INT);
                    dec = CalcHelpers.bin2Dec(bin, (currentView as ProgrammerView).currentBits);
                }

                // if we're going up in bitness
                else
                {
                    dec = changeBitnessHelper(bin, dec, INT, (currentView as ProgrammerView).currentBits, prevBits);
                }
            }
            else if ((currentView as ProgrammerView).currentBits == CalcViews.Programmer.ProgrammerCalcBits.Word)
            {
                // if we're going down in bitness
                if (bin.Length > SHORT)
                {
                    bin = bin.Substring(bin.Length - SHORT);
                    dec = CalcHelpers.bin2Dec(bin, (currentView as ProgrammerView).currentBits);
                }

                // if we're going up in bitness
                else
                {
                    dec = changeBitnessHelper(bin, dec, SHORT, (currentView as ProgrammerView).currentBits, prevBits);
                }
            }
            else if ((currentView as ProgrammerView).currentBits == CalcViews.Programmer.ProgrammerCalcBits.Byte)
            {
                // if we're going down in bitness
                if (bin.Length > BYTE)
                {
                    bin = bin.Substring(bin.Length - BYTE);
                    //dec = mainPage.bin2Dec(bin);
                }

                // can't go smaller than byte
                dec = CalcHelpers.bin2Dec(bin, (currentView as ProgrammerView).currentBits);
            }

            Set(dec, GetLastInputType(), true);
        }