Bit string to hex string

  • 55
  • 0

外面都找不到只好自己寫(問號?)

  public string BStringToHexString(string BString)
        {
            //建立輸出字串
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < BString.Length - 1; i += 4) {
                
                switch (BString.Substring(i, 4)) {
                    case "0000":
                        sb.Append("0");
                        break;
                    case "0001":
                        sb.Append("1");
                        break;
                    case "0010":
                        sb.Append("2");
                        break;
                    case "0011":
                        sb.Append("3");
                        break;
                    case "0100":
                        sb.Append("4");
                        break;
                    case "0101":
                        sb.Append("5");
                        break;
                    case "0110":
                        sb.Append("6");
                        break;
                    case "0111":
                        sb.Append("7");
                        break;
                    case "1000":
                        sb.Append("8");
                        break;
                    case "1001":
                        sb.Append("9");
                        break;
                    case "1010":
                        sb.Append("A");
                        break;
                    case "1011":
                        sb.Append("B");
                        break;
                    case "1100":
                        sb.Append("C");
                        break;
                    case "1101":
                        sb.Append("D");
                        break;
                    case "1110":
                        sb.Append("E");
                        break;
                    case "1111":
                        sb.Append("F");
                        break;
                }            
            }
            return sb.ToString();
        }