2017-05-22 3 views
0

私は楽しいために圧縮アルゴリズムに取り組んでいますが、データをロードするコードの一部で作業していますクラスにエラーが発生しました。ここにエラーを返すコードがあります:"boolからsystem.collections.generic.listに変換できない" <bool> "良いコードと思われるもの

/// <summary> 
    /// Loads an amount of bits from the filestream 
    /// </summary> 
    /// <param name="fs">A file from which to read the bits</param> 
    /// <param name="amount">The amount of bits to read from</param> 
    /// <param name="firstByte">The first byte from which to read some bits</param> 
    /// <param name="firstByteOffset">The offset fot the first byte</param> 
    /// <returns>The next byte and its offset in a tuple</returns> 
    public Tuple<byte, int> loadBits(ref FileStream fs, int amount, byte firstByte, int firstByteOffset) 
    { 
     ///The bits to be added to the internal array 
     List<bool> toBeAdded = new List<bool>(); 
     ///Add the first byte's data 
     toBeAdded.AddRange(splitList(new List<bool>(convertByte8(firstByte)), firstByteOffset)[1]); 
     ///Add the regular data in the center 
     for(int i = 0; i!= (int)Math.Floor((double)amount - 1)/8; i++) 
     { 
      toBeAdded.AddRange(convertByte8((byte)fs.ReadByte())); 
     } 
     ///Create a Tuple that contains the last byte and what it should be offsetted by the next time this method is run 
     Tuple<byte, int> rtrnVal = new Tuple<byte, int>((byte)fs.ReadByte(), amount - ((int)Math.Floor((double)amount - 1)/8) * 8); 
     ///Add the bits from the last byte 
     toBeAdded.AddRange(splitList(new List<bool>(splitList<bool>(/* error here */new List<bool>(convertByte8(rtrnVal.Item1))[0]/* To here */, rtrnVal.Item2)))); 
     ///Return 
     return rtrnVal; 
    } 

エラーはメソッドの2番目の行にあります。最後のバイトのビットにオフセットを適用して追加するだけです。私はそれが必要だとは思わないが、ここで非ネイティブの二つの方法が(のように私はそれらを作った)は次のとおりです。

/// <summary> 
    /// Splits a list into 2 
    /// </summary> 
    /// <typeparam name="T">The type of the list</typeparam> 
    /// <param name="inList">The list to be split</param> 
    /// <returns>2 List of half the size each</returns> 
    public static List<List<T>> splitList<T>(List<T> inList, int splitIndex = 0) 
    { 
     splitIndex = Math.Abs(splitIndex); 
     if (splitIndex == 0) 
     { 
      List<List<T>> newLists = new List<List<T>>(2); 
      newLists[0] = new List<T>((int)Math.Ceiling((double)inList.Count/2)); 
      newLists[1] = new List<T>((int)Math.Floor((double)inList.Count/2)); 
      for (int i = 0; i != inList.Count; i++) 
      { 
       if (i > (int)Math.Ceiling((double)inList.Count/2)) 
       { 
        newLists[1][i - (int)Math.Ceiling((double)inList.Count/2)] = inList[i]; 
        continue; 
       } 
       newLists[0][i] = inList[i]; 
      } 
      return newLists; 
     } 
     else 
     { 
      List<List<T>> newLists = new List<List<T>>(2); 
      newLists[0] = new List<T>(splitIndex); 
      newLists[1] = new List<T>(inList.Count - splitIndex); 
      for (int i = 0; i != inList.Count; i++) 
      { 
       if (i > splitIndex) 
       { 
        newLists[1][i - splitIndex] = inList[i]; 
        continue; 
       } 
       newLists[0][i] = inList[i]; 
      } 
      return newLists; 
     } 
    } 
    /// <summary> 
    /// Converts a byte to 8 bits 
    /// </summary> 
    /// <param name="inByteA">The byte to be converted to bits</param> 
    /// <returns>The 8 bit representation of inByteA</returns> 
    static public bool[] convertByte8(byte inByteA) 
    { 
     bool[] result = new bool[8]; 
     string a = Convert.ToString(inByteA, 2); 
     int temp = 0; 
     foreach (char b in a) 
     { 
      result[temp] = b == '0' ? false : true; 
      temp++; 
     } 
     return result; 
    } 

はあなたがかもしれないというのが私のコードでがあるかもしれないすべてのエラーのために事前にありがとう見つける。

PS。変数の後の?は何を意味しますか?

+0

@Xaqronいいえ変数を宣言した後、つまり、 'int? A ' –

+0

は、変数型のnull可能なバージョンを作成していることを意味します。 int a = null;壊れるだろう。 int? a =ヌル。完璧です。 (https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/nullable-types/using-nullable-types) – Ceisc

答えて

2

問題は、失敗した部分(new List<bool>(convertByte8(rtrnVal.Item1))[0])が単一のbool(convertByte8によって返された配列の最初のブール)を参照することです。

トラブルシューティングが難しい理由の1つは、回線に深くネストされたコールが失敗したためです。私は、少なくとも最初は、これらの呼び出しを小さなステップに分割し、戻り値をローカル変数に格納し、それらの変数を次の呼び出しに渡すことをお勧めします。このアプローチは、より冗長ではあるが、実質的にトラブルシューティングを容易にします。

0

私たちは小さな塊

toBeAdded.AddRange(splitList(new List<bool>(splitList<bool>(new List<bool>(convertByte8(rtrnVal.Item1))[0], rtrnVal.Item2)))); 

に次の行を行ってブレークした場合、我々はあなたがbool型のリストを作成しての最初の要素を取っているのvar日間

var d = new List<bool>(convertByte8(rtrnVal.Item1))[0]; 
var c = splitList<bool>(d, rtrnVal.Item2); 
var b = new List<bool>(c); 
var a = splitList(b); 
toBeAdded.AddRange(a); 

になるだろうそれは単なるブールです。これをsplitList()の最初のパラメータに渡します。このパラメータはboolパラメータをとりません。これは、bool型のリストを予期しています。

これでエラーが発生しています。

コードの目的がわからないため、修正するために必要なものを提案できるとは思えませんが、これが原因でエラーが発生していることがわかりました。

関連する問題