Ho ottenuto una lista booleana con 92 booleani, voglio che la lista sia convertita in una stringa, ho pensato di prendere 8 booleani (bit) e metterli in un byte (8 bit) e poi usa ASCII per convertirlo in un char valore in byte e poi aggiungi i char in una stringa. Tuttavia dopo aver fatto googeling per più di 2 ore, nessuna fortuna. Ho provato a convertire la lista in una lista Byte ma non funzionava neanche ^^.Converti lista <boolean> in stringa
String strbyte = null;
for (int x = 0; x != tmpboolist.Count; x++) //tmpboolist is the 90+- boolean list
{
//this loop checks for true then puts a 1 or a 0 in the string(strbyte)
if (tmpboolist[x])
{
strbyte = strbyte + '1';
}
else
{
strbyte = strbyte + '0';
}
}
//here I try to convert the string to a byte list but no success
//no success because the testbytearray has the SAME size as the
//tmpboolist(but it should have less since 8 booleans should be 1 Byte)
//however all the 'Bytes' are 48 & 49 (which is 1 and 0 according to
//http://www.asciitable.com/)
Byte[] testbytearray = Encoding.Default.GetBytes(strbyte);
PS Se qualcuno ha una migliore suggerimento su come codificare & decodificare un elenco booleana ad una stringa? (Perché voglio che le persone condividano la loro lista booleana con una stringa piuttosto che una lista di 90 1 e 0).
EDIT: funziona ora! ty tutto per aiutare
string text = new string(tmpboolist.Select(x => x ? '1' : '0').ToArray());
byte[] bytes = getBitwiseByteArray(text); //http://stackoverflow.com/a/6756231/1184013
String Arraycode = Convert.ToBase64String(bytes);
System.Windows.MessageBox.Show(Arraycode);
//first it makes a string out of the boolean list then it uses the converter to make it an Byte[](array), then we use the base64 encoding to make the byte[] a String.(that can be decoded later)
I ll guardo nella encoding32 tardi, ty per tutto l'aiuto ancora una volta :)
è necessario essere più specifico su ciò che si desidera che la stringa a guardare come –
Non è chiaro che cosa si vuole finire con. Una stringa che è una codifica ascii di bit, che può quindi essere trasformata in booleani? – jlew
'Encoding.Default.GetBytes' non fa quello che pensi che faccia. Vedi questa domanda: http://stackoverflow.com/questions/2989695/how-to-convert-a-string-of-bits-to-byte-array –