In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "how to convert BitArray into int and string", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to convert BitArray into int and string" article.
The foundation of BitArray can be seen by rookie programming.
The BitArray class manages a compact array of bit values represented by Boolean values, where the true bit is on (1) and the false bit is off (0).
Use a point array when you need to store bits but do not know the number of bits in advance. You can access items from the dot array collection using an integer index, starting from scratch.
Methods and properties of the BitArray class
The following table lists some common properties of the BitArray class:
Attribute description Count gets the number of elements contained in the BitArray. IsReadOnly gets a value indicating whether BitArray is read-only. Item gets or sets the value of the bit at the specified position in the BitArray. Length gets or sets the number of elements in the BitArray.
The following table lists some common methods of the BitArray class:
Serial number method name & description 1public BitArray And (BitArray value)
Performs bitwise and operations on the elements in the current BitArray and the corresponding elements in the specified BitArray. 2public bool Get (int index)
Gets the value of the bit at the specified position in the BitArray. 3public BitArray Not ()
Reverses the bit value in the current BitArray so that the element set to true becomes false and the element set to false becomes true. 4public BitArray Or (BitArray value)
Performs bitwise or operations on elements in the current BitArray and corresponding elements in the specified BitArray. 5public void Set (int index, bool value)
Sets the bit at the specified position in the BitArray to the specified value. 6public void SetAll (bool value)
Sets all bits in BitArray to the specified value. 7public BitArray Xor (BitArray value)
Performs bitwise XOR operations on elements in the current BitArray and corresponding elements in the specified BitArray.
After getting a general idea of BitArray, let's convert BitArray to Int and string.
Transforming BitArray into Int method
/ Point matrix is converted to int/// public static int BitArrayToInt (BitArray bit) {int [] res = new int [1]; for (int I = 0; I < bit.Count; iTunes +) {bit.CopyTo (res, 0);} return res [0];}
Convert BitArray to string method (if there is garbled code, please modify the code yourself)
/ Point matrix is converted to string/// public static string BitArrayToStr (BitArray ba) {byte [] strArr = new byte [ba.Length / 8]; for (int I = 0; I < ba.Length / 8; iTunes +) {for (int index = I * 8, m = 1; index < I * 8 + 8; index++, m * = 2) {strArr [I] + = ba.Get (index)? (byte) m: (byte) 0;}} return Encoding.Default.GetString (strArr);}
Invoke the test on LinqPad (Dump is the output method of LinqPad):
Void Main () {var bi = new BitArray (new bool [] {true, false, true, false, false}); bool [] bs = bi.Cast (). ToArray (); / / directly into the bool array, but this result is meaningless BitArrayToInt (bi) .dump ("point matrix to int:"); / / point matrix to int BitArrayToStr (bi) .dump ("point matrix to string:") / / Point matrix to string / / Test converts the string to point matrix and then back to the string var str = "Tao Shen"; / / Point matrix only supports bool [], byte [], int [], so the string needs to be converted to byte [] byte [] byteArray = System.Text.Encoding.Default.GetBytes (str); var ba = new BitArray (byteArray) / / create a point matrix BitArrayToStr (ba) .dump ("convert point matrix to string:"); / / convert point matrix to string}
Results:
The above is about the content of this article on "how to convert BitArray into int and string". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.