J'ai un tableau de bits que j'essaye de convertir en tableau d'octets. Mais j'ai du mal à trouver la bonne logique pour cela.BitArray to ByteArray ne renvoie pas les données d'octet correct
Ceci est mes données de tableau de bits:
1111 11111111 11111111 11111111 11101101
Avec un résultat final de:
[0]: 00011010 //this is obviously wrong should be: 11101101
[1]: 11111111
[2]: 11111111
[3]: 11111111
[4]: 11111111 // also obviously wrong should be :00001111
Ceci est clairement pas juste, mais ne peut pas comprendre la logique à cet endroit précis.
Ceci est ma méthode dans ma classe bitstream:
public void GetByteArray(byte[] buffer)
{
//1111 11111111 11111111 11111111 11101101
int totalBytes = Mathf.CeilToInt(Pointer/8f);
int pointerPos = Pointer - 1; // read back should not change the current bitstream index
for (int i = totalBytes-1; i >= 0; --i)
{
int counter = 0; // next byte when == 8
for (int j = pointerPos; j >= 0; --j)
{
counter++;
pointerPos--;
if (BitArray[j]) // if bit array [j] is true then `xor` 1
buffer[i] |= 1;
// we don't shift left for counter==8 to avoid adding extra 0
if (counter < 8)
buffer[i] = (byte)(buffer[i] << 1);
else
break; //next byte
}
}
}
Peut-on voir où ma logique ne va pas ici?
thats satanés Ah embarrassant, heures gaspillées sur ce qu'il soit prévu dans une ligne. lol Merci pour ça. Yat-il un moyen de voir le code source de CopyTo je suis curieux de savoir comment ils l'ont fait pour que je puisse voir où je me suis trompé. – Sir
Google est votre ami :). – NightOwl888
Voici comment il est implémenté dans .NET Core: https://github.com/dotnet/corefx/blob/master/src/System.Collections/src/System/Collections/BitArray.cs#L509, et voici. Cadre NET: http://www.dotnetframework.org/default.aspx/DotNET/DotNET/[email protected]/untmp/whidbey/REDBITS/ndp/clr/src/BCL/System/Collections/[email protected]/1/BitArray @cs – NightOwl888