Esempio n. 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);
        }
Esempio n. 2
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);
        }