Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以二进制格式保存字符串数组_C#_String_Binaryfiles - Fatal编程技术网

C# 以二进制格式保存字符串数组

C# 以二进制格式保存字符串数组,c#,string,binaryfiles,C#,String,Binaryfiles,我正在寻找最快的方法将每个字符串转换为每个字符串定义长度的BitArray(如果字符串大于定义长度,则应修剪该字符串,如果字符串大小较小,则剩余的位将填充为false),然后将这两个字符串放在一起,并将其写入二进制文件 编辑: @dtb:一个简单的例子可以是这样的:value1=“a”,value2=“B”,length1=8,length2=16,结果将是010000010000000000010 前8位来自“A”,接下来的16位来自“B”。当将字符串转换为其他的东西时,需要考虑使用什么编码。

我正在寻找最快的方法将每个字符串转换为每个字符串定义长度的BitArray(如果字符串大于定义长度,则应修剪该字符串,如果字符串大小较小,则剩余的位将填充为false),然后将这两个字符串放在一起,并将其写入二进制文件

编辑: @dtb:一个简单的例子可以是这样的:value1=“a”,value2=“B”,length1=8,length2=16,结果将是010000010000000000010
前8位来自“A”,接下来的16位来自“B”

。当将字符串转换为其他的东西时,需要考虑使用什么编码。这是一个使用UTF-8的版本

string value1 , value1 ;
int length1 , length2 ;
System.Collections.BitArray bitValue1 = new System.Collections.BitArray(Length1);
System.Collections.BitArray bitValue2 = new System.Collections.BitArray(Length2);
编辑
隐马尔可夫模型。。。看到您正在寻找的是位数组而不是ByteArray,这可能对您没有帮助。

因为这不是一个非常明确的问题,所以我会尝试一下

bitValue1 = System.Text.Encoding.UTF8.GetBytes(value1, 0, length1);
使用System.IO; 使用System.Runtime.Serialization; 使用System.Runtime.Serialization.Formatters.Binary; 公共静态void RunSnippet() { 字符串s=“123”; byte[]b=System.Text.ascienceoding.ASCII.GetBytes(s); System.Collections.BitArray bArr=新的System.Collections.BitArray(b); WriteLine(“bArr.Count={0}”,bArr.Count); for(int i=0;i 希望这有帮助, 顺致敬意, Tom.

//源字符串
字符串值1=“t”;
//以位为单位的长度
int长度1=2;
//将文本转换为ASCII字节数组
byte[]bytes=System.Text.Encoding.ASCII.GetBytes(value1);
//从字节创建临时位数组
System.Collections.BitArray tempBits=新的System.Collections.BitArray(字节);
//创建设置最大长度的输出位数组
System.Collections.BitArray bitValue1=新的System.Collections.BitArray(长度1);
//循环遍历临时数组

对于(int i=0),为什么需要位数组?如果有其他方法,我不需要位数组。但是考虑长度可以是任何数字(可能是2位)。字符串的内容是什么?能否请您附上一些代码,以解释更多关于您要为我们实现什么?SooEs?字符串可以包含任何东西。(整数、布尔值、字符串……但都转换为字符串)你能发布输入和预期输出的示例吗?长度1是位数。谢谢你,cheris,我编辑了一点代码,现在它工作正常。顺便说一句,我发现这可能有助于其他想要保存二进制数据的人: 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(); }
        //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));                
        }