2010年12月16日 星期四

十六進制轉八進制使用 C#

16進制轉成8進制,可以透過C# 的內建函數解決 Convert.toString()

如果想要實做的話就先了解如何轉換,首先 16 進制顧名思義就是遇到16就進位

所以可以表示的範圍為1 ~ 15, 但是10 ~ 15 以 A ~ F 取代

以下範例 先將 16 轉成 2 進制 > 針對每個16進制的數字 拆解成4個位元以表示2進制

 F > 1111 , 8 > 1000

最後再將 2 進制轉成 8 進制 > 針對二進位中從尾巴開始數每三個數字及組合成一個8進制數字
如果不足及補零

10101100 每三個分隔 > 10|101|100 > 不足則補零 > 010|101|100 > 轉8進制 > 254

以下為程式實做

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            String line;
            while ((line = Console.ReadLine()) != null)
            {
                String[] binary = new String[16];   //最多接受輸入 16 個數字
                String binaryTMP = "";    //輸入的16進位轉2進位
                for (int i = 0; i < line.Length; i++)
                {
                    String data = line.Substring(i, 1);   //從頭開始切割
                    if (data.Equals("0")){   //0
                        //number[i] = 0;
                        binary[i] = "0000";binaryTMP += binary[i];
                        continue;
                    }
                    else if(data.Equals("1")){   //1
                        //number[i] = 1;
                        binary[i] = "0001";binaryTMP += binary[i];
                        continue;
                    }
                    else if(data.Equals("2")){  //2
                        //number[i] = 2;
                        binary[i] = "0010";binaryTMP += binary[i];
                        continue;
                    }
                    else if(data.Equals("3")){  //3
                        //number[i] = 3;
                        binary[i] = "0011";binaryTMP += binary[i];
                        continue;
                    }
                    else if(data.Equals("4")){  //4
                        //number[i] = 4;
                        binary[i] = "0100";binaryTMP += binary[i];
                        continue;
                    }
                    else if(data.Equals("5")){   //5
                        //number[i] = 5;
                        binary[i] = "0101";binaryTMP += binary[i];
                        continue;
                    }
                    else if(data.Equals("6")){   //6
                        //number[i] = 6;
                        binary[i] = "0110";binaryTMP += binary[i];
                        continue;
                    }
                    else if(data.Equals("7")){    //7
                        //number[i] = 7;
                        binary[i] = "0111";binaryTMP += binary[i];
                        continue;
                    }
                    else if(data.Equals("8")){   //8
                        //number[i] = 8;
                        binary[i] = "1000";binaryTMP += binary[i];
                        continue;
                    }
                    else if(data.Equals("9")){    //9
                        //number[i] = 9;
                        binary[i] = "1001";binaryTMP += binary[i];
                        continue;
                    }
                    else if(data.Equals("A") || data.Equals("a")){   //10
                        //number[i] = 10;
                        binary[i] = "1010";binaryTMP += binary[i];
                        continue;
                    }
                    else if(data.Equals("B") || data.Equals("b")){   //11
                        //number[i] = 11;
                        binary[i] = "1011";binaryTMP += binary[i];
                        continue;
                    }
                    else if(data.Equals("C") || data.Equals("c")){   //12
                        //number[i] = 12;
                        binary[i] = "1100";binaryTMP += binary[i];
                        continue;
                    }
                    else if(data.Equals("D") || data.Equals("d")){   //13
                        //number[i] = 13;
                        binary[i] = "1101";binaryTMP += binary[i];
                        continue;
                    }
                    else if(data.Equals("E") || data.Equals("e")){   //14
                        //number[i] = 14;
                        binary[i] = "1110";binaryTMP += binary[i];
                        continue;
                    }
                    else if(data.Equals("F") || data.Equals("f"))   //15
                    {
                        binary[i] = "1111";binaryTMP += binary[i];

                    }

                    
                }
                Console.WriteLine(binaryTMP + "   ");   //二進制
                char[] tempCharArr = binaryTMP.ToCharArray();   //將他轉成字元陣列
                int[] eight = new int[16];
                int pos = 0;
                String n = "";
                for (int i = tempCharArr.Length-1; i >= 0; i -= 3) //由尾巴開始往回取 一次三個 
                {
                    if (i == 1)   //不足一個 0
                    {
                        n = "0" + tempCharArr[i - 1] + tempCharArr[i];
                    }
                    else if (i == 0)  //不足兩個 0
                    {
                        n = "00" + tempCharArr[i];
                    }
                    else
                    {
                        n = (tempCharArr[i - 2].ToString()) + (tempCharArr[i - 1].ToString()) + (tempCharArr[i].ToString());
                    }
                    //以下將 n 這個二進制轉成八進制
                    if (n.Equals("000")){
                        eight[pos] = 0; pos++;
                        continue;
                    }
                    else if (n.Equals("001"))
                    {
                        eight[pos] = 1; pos++;
                        continue;
                    }
                    else if (n.Equals("010"))
                    {
                        eight[pos] = 2; pos++;
                        continue;
                    }
                    else if (n.Equals("011"))
                    {
                        eight[pos] = 3; pos++;
                        continue;
                    }
                    else if (n.Equals("100"))
                    {
                        eight[pos] = 4; pos++;
                        continue;
                    }
                    else if (n.Equals("101"))
                    {
                        eight[pos] = 5; pos++;
                        continue;
                    }
                    else if (n.Equals("110"))
                    {
                        eight[pos] = 6; pos++;
                        continue;
                    }
                    else if (n.Equals("111"))
                    {
                        eight[pos] = 7; pos++;
                        continue;
                    }
                    n = "";

                }
                for (int i = pos-1; i >=0; i--)
                {

                        Console.Write(eight[i]);

                }

            }
            

        }
    }
}

沒有留言:

張貼留言