私は楽しいために圧縮アルゴリズムに取り組んでいますが、データをロードするコードの一部で作業していますクラスにエラーが発生しました。ここにエラーを返すコードがあります:"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。変数の後の?
は何を意味しますか?
@Xaqronいいえ変数を宣言した後、つまり、 'int? A ' –
は、変数型のnull可能なバージョンを作成していることを意味します。 int a = null;壊れるだろう。 int? a =ヌル。完璧です。 (https://docs.microsoft.com/en-us/dotnet/articles/csharp/programming-guide/nullable-types/using-nullable-types) – Ceisc