开发者

save string array in binary format

开发者 https://www.devze.com 2022-12-16 20:04 出处:网络
string value1 , value1 ; int length1 , length2 ; System.Collections.BitArray bitValue1 = new System.Collections.BitArray(Length1);
string value1 , value1 ;
int length1 , length2 ;
System.Collections.BitArray bitValue1 = new System.Collections.BitArray(Length1);
System.Collections.BitArray bitValue2 = new System.Collections.BitArray(Length2);

I'm looking for the fastest way to covert each string to BitArray with defined length for each string (the string should be trimmed if it is larger than defined length and if strings size is smaller remaining bits will be filled with false) and then put this two strings together and write it in a binary file .

Edit : @dtb : a simple example can be like this value1 = "A" ,value2 = "B" and length1 =8 and length2 = 16 and the result will be 010000010000000001000010 the first 8 bits are from "A" and next 16 bits f开发者_运维问答rom "B"


        //Source string
        string value1 = "t";
        //Length in bits
        int length1 = 2;
        //Convert the text to an array of ASCII bytes
        byte[] bytes = System.Text.Encoding.ASCII.GetBytes(value1);
        //Create a temp BitArray from the bytes
        System.Collections.BitArray tempBits = new System.Collections.BitArray(bytes);
        //Create the output BitArray setting the maximum length
        System.Collections.BitArray bitValue1 = new System.Collections.BitArray(length1);
        //Loop through the temp array
        for(int i=0;i<tempBits.Length;i++)
        {
            //If we're outside of the range of the output array exit
            if (i >= length1) break;
            //Otherwise copy the value from the temp to the output
            bitValue1.Set(i, tempBits.Get(i));                
        }

And I'm going to keep saying it, this assumes ASCII characters so anything above ASCII 127 (such as the é in résumé) will freak out and probably return ASCII 63 which is the question mark.


When converting a string to something else you need to consider what encoding you want to use. Here's a version that uses UTF-8

bitValue1 = System.Text.Encoding.UTF8.GetBytes(value1, 0, length1);

Edit Hmm... saw that you're looking for a BitArray and not a ByteArray, this won't help you probably.


Since this is not a very clear question I'll give this a shot nonetheless,

using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
public static void RunSnippet()
{
   string s = "123";
   byte[] b = System.Text.ASCIIEncoding.ASCII.GetBytes(s);
   System.Collections.BitArray bArr = new System.Collections.BitArray(b);
   Console.WriteLine("bArr.Count = {0}", bArr.Count);
   for(int i = 0; i < bArr.Count; i++)
    Console.WriteLin(string.Format("{0}", bArr.Get(i).ToString()));
   BinaryFormatter bf = new BinaryFormatter();
   using (FileStream fStream = new FileStream("test.bin", System.IO.FileMode.CreateNew)){
    bf.Serialize(fStream, (System.Collections.BitArray)bArr);
    Console.WriteLine("Serialized to test.bin");
   }
   Console.ReadLine();
}

Is that what you are trying to achieve?

Hope this helps, Best regards, Tom.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号