我買了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所有幫助:)
你需要在你希望你的字符串看起來像 – 2012-02-10 18:54:57
目前尚不清楚你想要落得什麼什麼更具體。一個字符串,它是位的ASCII編碼,然後可以轉換回布爾值? – jlew 2012-02-10 18:56:47
'Encoding.Default.GetBytes'不會做你認爲它做的事。看到這個問題:http://stackoverflow.com/questions/2989695/how-to-convert-a-string-of-bits-to-byte-array – 2012-02-10 18:57:37