Create Octal To Hexadecimal Converter in C#

Posted by ေတဇာလင္း Thursday 29 September 2016 0 comments

မိတ္ေဆြ၊ ညီအစ္ကို၊ ေမာင္ႏွမအားလံုးပဲ မဂၤလာပါဗ်ာ။ ဒီေန႔ေတာ့ က်ေနာ္တို႔ Octal Number တစ္ခုကို Hexadecimal Number အျဖစ္ ဘရ္လို ေျပာင္းလဲ ယူႏိုင္မလဲဆိုတာေလးကို ေလ့လာၾကည့္ၾကမွာ ျဖစ္ပါတရ္။ Coding ေရးသားတဲ့အခါမွာ Visual Studio က ေထာက္ပံ့ေပးထားတဲ့ Method ေတြကို အသံုးမျပဳပဲ Template Matching and Repeated Divison Method ႏွစ္ခုကို ေပါင္းစပ္ၿပီး အလြယ္ကူဆံုးနည္းလမ္းတစ္ခုနဲ႔ ကိုယ္ပိုင္ Method တစ္ခု တည္ေဆာက္ကာ ေလ့လာၾကည့္ၾကမွာျဖစ္ပါတရ္။ Logic ပိုင္း ေထြေထြထူးထူးမရွိပါဘူး။ သာမာန္အေတြးကိုပဲ ရုပ္လံုးေဖာ္လုိက္တာပါ။ Coding Flow ေလးကိုၾကည့္ၾကည့္မရ္ဗ်ာ။
  1. ေျပာင္းလဲတဲ့အခါမွာ Octal to Binary အျဖစ္ အရင္ေျပာင္းလဲမွာျဖစ္ၿပီး ၄င္း ရရွိလာတဲ့ Binary Number ကိုမွ Hexadecimal Number အျဖစ္ ေျပာင္းလဲယူမွာျဖစ္ပါတရ္။
  2. Octal ကို Binary ေျပာင္းတဲ့အခါမွာေတာ့ 10 နဲ႔စားကာ ကိန္းစဥ္တန္းရဲဲ႕ေနာက္ဆံုးတစ္လံုးကိုျဖတ္ယူပါတရ္။ ၄င္းရရွိလာတဲ့ Number ကိုမွ Template Matching Method ကိုအသံုးျပဳၿပီး က်ေနာ္တို႔ ႀကိဳတင္သတ္မွတ္ထားတဲ့ OctalArray နဲ႔ တိုက္ဆိုင္စစ္ေဆးပါတရ္။ ရရွိလာတဲ့ တန္ဖိုးေတြကိုမွ Loop -- False ျဖစ္တဲ့အထိ စုစည္းယူလိုက္ၿပီး Binary Number အျဖစ္ ဖန္တီးယူလိုက္တာပါ။
  3. Binary ကို Hexadecimal ေျပာင္းတဲ့အခါမွာေတာ့ (၄)လံုးအတြဲခြဲထုတ္မွာျဖစ္လို႔ 10000 နဲ႔ စားပါတရ္။ ရလာတဲ့ Binary (၄)လံုးတြဲေတြကိုမွ if() statement ကိုျဖတ္ၿပီး Template Matching Method သေဘာနဲ႔ပဲ တိုက္ဆိုင္စစ္ေဆးပါတရ္။ ေနာက္ဆံုးမွာေတာ့ loop -- False ျဖစ္တဲ့အထိ ပါတ္ၿပီး loop အႀကိမ္တိုင္းမွာ ရလာတဲ့ Hex value ေတြကို စုစည္းေဖာ္ျပေပးျခင္းျဖင့္ က်ေနာ္တို႔ လိုခ်င္ေနတဲ့ Hexadecimal Number တစ္ခုကို ဖန္တီးယူလိုက္တာပဲျဖစ္ပါတရ္ဗ်ာ။
နားလည္ႏိုင္ၾကလိမ့္မရ္လို႔ ထင္ပါတရ္ဗ်ာ။ Logic အနည္းငယ္စိမ္းေနမရ္ဆိုရင္ေတာ့..... http://programmingknowledge.blogspot.com/2014/07/7-number-system-conversions-from-octal.html Page မွာ ပံုနဲ႔တကြ ရွင္းထားတာေလးရွိပါတရ္။ ေလ့လာၾကည့္ႏိုင္ပါတရ္ဗ်ာ။ အခုေတာ့ Coding အပိုင္းေလး ဆက္ၾကမရ္။ GUI ကိုေတာ့ က်ေနာ္ ေအာက္က ပံုစံအတိုင္း ပံုေဖာ္ထားပါတရ္။ မိတ္ေဆြတို႔ေတာ့ စိတ္ႀကိဳက္ ပံုေဖာ္ ဖန္တီးႏိုင္ပါတရ္။
ပိုၿပီး နားလည္သြားေအာင္ Coding အျပည့္အစံုေလးကို ေအာက္မွာ ေလ့လာၾကည့္လိုက္ၾကပါအံုးဗ်ာ။
using System;
using System.Windows.Forms;

namespace NumberTypeConverter
{
    public partial class Octal_Conversion : Form
    {
        public Octal_Conversion()
        {
            InitializeComponent();
        }

        public string OctalToHexadecimal(int Oct)
        {
            //Octal to Binary
            string[] OctalConstant = { "000", "001", "010", "011", "100", "101", "110", "111" };
            int OctIndex, quotient = Oct;
            string binary = null, Hex = null, result=null;
            while (quotient != 0)
            {
                OctIndex = quotient % 10;
                if (OctIndex >= 8)
                {
                    MessageBox.Show("It's not Octal Number");
                    goto End;
                }
                else
                {
                    binary = OctalConstant[OctIndex] + "" + binary;
                    quotient /= 10;
                }           
            }
            
            //Binary To Hex  
            long fourDigit, bi = long.Parse(binary);
            while (bi != 0)
            {
                fourDigit = bi % 10000;
                if (fourDigit == 0) Hex = "0";
                if (fourDigit == 1) Hex = "1";
                if (fourDigit == 10) Hex = "2";
                if (fourDigit == 11) Hex = "3";
                if (fourDigit == 100) Hex = "4";
                if (fourDigit == 101) Hex = "5";
                if (fourDigit == 110) Hex = "6";
                if (fourDigit == 111) Hex = "7";
                if (fourDigit == 1000) Hex = "8";
                if (fourDigit == 1001) Hex = "9";
                if (fourDigit == 1010) Hex = "A";
                if (fourDigit == 1011) Hex = "B";
                if (fourDigit == 1100) Hex = "C";
                if (fourDigit == 1101) Hex = "D";
                if (fourDigit == 1110) Hex = "E";
                if (fourDigit == 1111) Hex = "F";
                result = Hex+""+result;
                bi /= 10000;            
            }
            End:
            return result;       
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            if (txtOctal.Text !="")
            {
                txtHexadecimal.Text = OctalToHexadecimal(int.Parse(txtOctal.Text));                
            }
            txtOctal.Focus();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtOctal.Clear();
            txtHexadecimal.Clear();
            txtOctal.Focus();
        }   
    }
} 
ရွင္းလင္းခ်က္နဲ႔ Screen Shoot ေလးေတြကို ၾကည့္လိုက္ရင္ေတာ့ နားလည္သြားၾကလိမ့္မရ္လို႔ ထင္ပါတရ္ဗ်ာ။ မိတ္ေဆြတို႔အေနနဲ႔ေတာ့ Trace လိုက္ၿပီး Coding Flow ေလးကို ေလ့လာၾကည့္ေစခ်င္ပါတရ္။ မိတ္ေဆြ၊ ညီအစ္ကို၊ ေမာင္ႏွမအားလံုး ေလ့လာျခင္းျဖင့္ ေက်နပ္ႏိုင္ၾကပါေစဗ်ာ။

0 Responses so far.

Post a Comment