2017-05-30 19 views
0

我真的無法解決這個問題。我有一個字典,我已經保存了一個編碼表。我正在嘗試爲密鑰獲得正確的值。 帶有不同where子句的if-子句應該100%導致結果,但它不。 where子句被忽略了嗎?LINQ詞典中的Where-Clauses不能正常工作

public static string ByteArrayToHex(byte[] bytes) 
    {    
      StringBuilder Result = new StringBuilder(bytes.Length * 2); 
      string HexAlphabet = "ABCDEF"; 

      foreach (byte B in bytes) 
      { 
       Result.Append(HexAlphabet[(int)(B >> 4)]); 
       Result.Append(HexAlphabet[(int)(B & 0xF)]); 
      } 

      return Result.ToString(); 

    } 


static void Main(string[] args) 
    { 
     List<byte> byteList2 = new List<byte>() { 227, 129,130 }; 

     List<byte> byteList = new List<byte>() {131, 95, 131, 126, 129, 91}; 
     string filename = "file.txt"; 
     var dict = Shared.IO.Read.EncodingTable(filename); 

     var original = Encoding.GetEncoding(932).GetString(byteList.ToArray()); 
     var custom = CustomConverter(byteList, dict); 

     var original2 = Encoding.GetEncoding(932).GetString(byteList2.ToArray()); 
     var custom2 = CustomConverter(byteList2, dict); 

     Console.WriteLine("Original: " + original); 
     Console.WriteLine("Custom: " + custom); 
     Console.WriteLine("Original: " + original2); 
     Console.WriteLine("Custom: " + custom2); 

     Console.ReadKey(); 
    } 

    static string CustomConverter(List<byte> byteList, Dictionary<string,string> dict) 
    { 
     var result = string.Empty; 

     while (byteList.Count != 0) 
     { 
      var count = byteList.Count; 

      byte[] bytes = new byte[] { }; 
      if (count > 2) bytes = new byte[3] {byteList[0], byteList[1], byteList[2]}; 
      if (count == 2) bytes = new byte[2] {byteList[0], byteList[1]}; 
      if (count == 1) bytes = new byte[1] {byteList[0]}; 

      var hexString = Shared.IO.Convert.ByteArrayToHex(bytes); 
      KeyValuePair<string,string> entry; 



      if (hexString.Length > 2) 
      { 
       var a = hexString.Substring(0, 4); 
       var b = hexString.Substring(0, 2); 
       var c = hexString.Substring(0, 1); 

       entry = (from item in dict 
        where item.Key == hexString || item.Key == a || item.Key == b || item.Key == c 
        select item).First(); 
      } 

      else 
      { 
       var a = hexString.Substring(0, 1); 

       entry = (from item in dict 
       where item.Key == hexString || item.Key == a 
       select item).First(); 
      } 
      byteList.RemoveRange(0, (entry.Key.Length/2)); 

      result = result + entry.Value; 
     } 

     return result; 
}` 

public static Dictionary<string, string> EncodingTable(string filename) 
    { 
     string directory = Environment.CurrentDirectory + @"\encoding\"; 
     string path = directory + filename; 

     Dictionary<string,string> encodingTable = new Dictionary<string, string>(); 

     var lines = System.IO.File.ReadAllLines(path); 

     foreach (var line in lines) 
     { 
      if (string.IsNullOrEmpty(line)) continue; 
      if (line.StartsWith("#")) continue; 

      var lineArray = line.Split(';'); 
      encodingTable.Add(lineArray[0], lineArray[1]); 
     } 

     return encodingTable; 
    }` 

字典包含這樣的事情

79;か 7A;き 7B;く 7C;け 7D;こ 7E;さ 7F;し 80;す 81 ;せ 82;そ 83;た 84;ち

+2

請發表[mcve]。我們需要查看該字典中的密鑰以及您嘗試匹配哪些字節。另外,發佈'ByteArrayToHex'返回的內容。 –

+0

你能用我們可以用來重現你的結果的東西來更新你的代碼示例嗎?實際上,我們不知道'hexString'的值是什麼,或者字典包含什麼。填充字典的簡短方法會很有幫助。 –

+0

「ByteArrayToHex」返回什麼?我假設這是一個字符串,因爲有'Substring'調用,但是如果長度大於2,則調用'Substring(0,1)'。是否檢查過'a','b','c'的值調試器確保它們是你期望它們的東西? –

回答

0

鑑於你的代碼,並建立與您提供圖片的數據file.txt的ided suplemented與

5F;_ 5B;[ E1;# E3;#

我得到的迴應:

Original: ダミー 
Custom: た _た さ せ [ 
Original: 縺・ 
Custom: #せ そ 

你看到不同的東西?或者這不是你想要的?

+0

是的,它看起來是正確的(沒有整個詞典知道)。 我的結果是 HEX:835F837E815B 原文:ダミー 定製:たêたさせ一個 HEX:E38182 原文:縺· 定製:せそ HEX:5F5BE1E3 原文:膺AQ7 自定義:?GOQ 我已經使用了另一種解決方案。 dict.TryGetValue,如果它是真的,我開始查詢。 – Insight

0

據我所嘗試的不同的事情,TryGetValue方法是比使用太多where-conditions更好的選擇。儘管表現非常糟糕。感謝提示「D斯坦利」。

目標是檢查字典(size =最大3字節),如果我的當前數組(轉換爲hexstring)在此字典中。如果是這種情況,它將返回輸出值。希望有人認爲它有用。

private static string _hexString = null; 
    public static string ByteValueToCustomEncoding(byte[] byteArray, Dictionary<string, string> dict) 
    { 
     var result = string.Empty; 

     _hexString = Shared.IO.Convert.ByteArrayToHex(byteArray); 

     while (_hexString.Length > 0) 
     { 
      string entry = string.Empty; 

      if (_hexString.Length >= 6) 
      { 
       entry = Helper(dict); 
      } 

      else if (_hexString.Length >= 4) 
      { 
       entry = Helper(dict); 
      } 

      else if (_hexString.Length >= 2) 
      { 
       entry = Helper(dict); 
      } 

      else if (_hexString.Length >= 1) 
      { 
       entry = Helper(dict); 
      } 

      result = result + entry; 
     } 

     return result; 
    } 

    private static string Helper(Dictionary<string,string> dict) 
    { 
     string temp = String.Empty; 

     for (int i = (_hexString.Length > 6) ? 6: _hexString.Length; i >= 0; i = i - 2) 
     { 
      if (dict.TryGetValue(_hexString.Substring(0, (i == 0) ? 1 : i), out temp)) 
      { 
       _hexString = _hexString.Remove(0, i); 
       return temp; 
      } 
     } 

     _hexString = _hexString.Remove(0, 1); 

     return temp; 
    }