2012-02-10 106 views
6

我買了92個布爾一個布爾值列表,我想要列表轉換爲字符串,我想我會坐8布爾(位),並把它們放在一個字節(8位),然後使用ASCII將字節值轉換爲字符,然後將字符添加到字符串中。然而,在經過了2個多小時後,沒有運氣。我試圖將列表轉換爲一個字節列表,但它沒有工作^^。轉換列表<boolean>爲String

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如果任何人有關於如何&解碼布爾列表代碼到一個字符串更好的建議? (因爲我想讓人們用一個字符串來分享它們的布爾列表,而不是一個90 1和0的列表。)

編輯:讓它現在工作! TY所有幫助

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) 

我會考慮的encoding32以後,再次TY所有幫助:)

+0

你需要在你希望你的字符串看起來像 – 2012-02-10 18:54:57

+0

目前尚不清楚你想要落得什麼什麼更具體。一個字符串,它是位的ASCII編碼,然後可以轉換回布爾值? – jlew 2012-02-10 18:56:47

+1

'Encoding.Default.GetBytes'不會做你認爲它做的事。看到這個問題:http://stackoverflow.com/questions/2989695/how-to-convert-a-string-of-bits-to-byte-array – 2012-02-10 18:57:37

回答

13

您應該將您的布爾值存儲在BitArray中。

var values = new BitArray(92); 
values[0] = false; 
values[1] = true; 
values[2] = true; 
... 

然後您可以將BitArray轉換爲字節數組

var bytes = new byte[(values.Length + 7)/8]; 
values.CopyTo(bytes); 

和字節數組到Base64串

var result = Convert.ToBase64String(bytes); 

相反地,你可以轉換爲Base64字符串到一個字節陣列

var bytes2 = Convert.FromBase64String(result); 

和字節數組到BitArray

var values2 = new BitArray(bytes2); 

的Base64的字符串如下所示:"Liwd7bRv6TMY2cNE"。這對於人們之間的分享可能有點不方便;看看human-oriented base-32 encoding

預期這些[鹼-32串]的用途包括cut- 和粘貼,文本編輯(例如在HTML文件),經由鍵盤 ,手動經由筆和紙,聲樂轉錄超過 電話或無線電轉錄手動轉錄等

對於這樣的編碼的必要條件是:

  • 最小化轉錄錯誤 - 例如衆所周知的將 '0'與'O'相混淆的問題嵌入到其他結構中 - 例如,搜索引擎,結構化或 標記文本,文件系統,命令shell
  • 簡潔 - 較短的[字符串]比較長的字符更好。
  • 人體工程學 - 人類用戶(特別是非技術人員)應該儘可能輕鬆愉快地找到 [字符串]。 [弦]看起來越醜,就越糟糕。
+0

乾杯,我不介意它是基地64我認爲,這是一個代碼,可以分享(99%將複製和粘貼),然後導入應用程序,他們將能夠看到他們有什麼項目(真)還是不(假)。 編輯:我不想將它存儲在BitArray中,因爲它可以增長(列表),因此大小可能會增加,因此使用List 更容易。右:p? – Maximc 2012-02-10 19:19:18

+0

以防萬一:[Base32Encoding實現爲.NET](http://hemant-jangid.blogspot.com/2009/06/base32encoding-implementation-for-net.html) – dtb 2012-02-10 19:20:10

+0

我使用一個布爾列表,所以第一部分doesn工作,但其餘的,但我仍然需要找到一種方法讓我的tmpboollist字節[]。 – Maximc 2012-02-10 20:05:01

1

首先,這是一個壞主意,連接字符串在這樣一個循環 - 至少使用StringBuilder,或使用類似這樣的LINQ:

string text = new string(tmpboolist.Select(x => x ? '1' : '0').ToArray()); 

但你的字符串轉換爲List<bool>很容易與LINQ,使用string實現IEnumerable<char>的事實:

List<bool> values = text.Select(c => c == '1').ToList(); 

目前還不清楚字節數組的位置在哪裏......但是您的should not try to represent arbitrary binary data in a string just using Encoding.GetString。這不是它的目的。

如果你不關心你的字符串使用什麼格式,那麼使用Base64可以很好地工作 - 但要注意,如果你將布爾值分組爲字節,如果你需要區分例如,「7值」和「8值,其中第一值爲假」。

+0

謝謝你的好幫手,現在我不需要循環了! :) – Maximc 2012-02-10 19:18:03

1

因爲我從你的代碼infering你想用1或0取決於onthe內部列表布爾值,那麼怎麼樣的n個數字組成的字符串...

public override string ToString() 
{ 
    StringBuilder output = new StringBuilder(91); 
    foreach(bool item in this.tempboolist) 
    { 
     output.Append(item ? "1" : "0"); 
    } 
    return output.ToString(); 
} 

警告,這是關袖口打字,我還沒有用編譯器驗證過!

0

此功能你想要做什麼:

public String convertBArrayToStr(bool[] input) 
    { 
     if (input == null) 
      return ""; 
     int length = input.Count(); 
     int byteArrayCount = (input.Count() - 1)/8 + 1; 
     var bytes = new char[byteArrayCount]; 

     for (int i = 0; i < length; i++) 
     { 
      var mappedIndex = (i - 1)/8; 
      bytes[mappedIndex] = (char)(2 * bytes[mappedIndex] +(input[i] == true ? 1 : 0)); 
     } 
     return new string(bytes); 
    } 
相關問題