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

发布于 2024-08-18 21:45:28 字数 500 浏览 6 评论 0原文

string value1 , value1 ;
int length1 , length2 ;
System.Collections.BitArray bitValue1 = new System.Collections.BitArray(Length1);
System.Collections.BitArray bitValue2 = new System.Collections.BitArray(Length2);

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

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

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 from "B"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北方的韩爷 2024-08-25 21:45:28
        //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));                
        }

我要继续说,这假设 ASCII 字符,因此任何高于 ASCII 127 的内容(例如简历中的 é)都会崩溃,并可能返回 ASCII 63,即问号。

        //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.

挽梦忆笙歌 2024-08-25 21:45:28

将字符串转换为其他内容时,您需要考虑要使用的编码。 的版本

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

这是使用 UTF-8 编辑
嗯...看到您正在寻找 BitArray 而不是 ByteArray,这可能对您没有帮助。

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.

秋日私语 2024-08-25 21:45:28

由于这不是一个非常明确的问题,尽管如此,我还是会尝试一下,

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();
}

这是您想要实现的目标吗?

希望这有帮助,
此致,
汤姆.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文