Java BitSet - 放置字符/字符串的包装器

发布于 2024-10-10 03:50:21 字数 628 浏览 2 评论 0原文

我正在使用 Java 中的 BitSet 来存储必须序列化到较低抽象级别或从较低抽象级别反序列化的值。这是为了创建一个文件格式的容器。

目前我想将字符串存储到 BitSet 中:

private BitSet string_to_bit(String content_string) {

        BitSet result = new BitSet(this.size);

        char[] content_string_arr = new char[content_string.length()];
        for (char c : content_string_arr) {
            int tmp = (int) c;
            result += Integer.toBinaryString(tmp);
        }
}

+= 在 BitSet 上未定义,因此此时代码是错误的。 Afaik 我无法重写 Java 6 中的运算符。您将如何优雅地包装该操作?我不想最终一遍又一遍地重复所有这些低级转换,因为它们似乎在重复。有 .toBit 等效项吗?我可以使用 toString() 将 BitSet 转换为 String。为什么没有去抽象?

最好的, 马吕斯

I'm making use of BitSet in Java to store values I have to serialize and deserialize into/from lower abstraction levels. This is to create a file-format container.

Currently I want to store a String into a BitSet:

private BitSet string_to_bit(String content_string) {

        BitSet result = new BitSet(this.size);

        char[] content_string_arr = new char[content_string.length()];
        for (char c : content_string_arr) {
            int tmp = (int) c;
            result += Integer.toBinaryString(tmp);
        }
}

The += is undefined on a BitSet, so the code is wrong at that point. Afaik I cannot override operators in Java 6. How would you elegantly wrap that operation? I don't want to end up repeating all these low level conversions over and over again because as it seems they repeat. Is there any .toBit equivalent? I can cast a BitSet to String with toString(). Why is there no de-abstraction?

Best,
Marius

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

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

发布评论

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

评论(4

甚是思念 2024-10-17 03:50:21

据我所知,没有 .toBit() 等效项。你应该编写自己的实现。

这里有两个链接可能对您有帮助 在 BitSet 和字节数组之间转换< /a>

将 bitset 转换为 int 数组和字符串

as far as I know there is not .toBit() equivalent. you should write your own implementtation.

Here are 2 links may be helpful to you that Converting Between a BitSet and a Byte Array

and Convert bitset to int array and string

╭ゆ眷念 2024-10-17 03:50:21

我发现你使用 BitSet 进行序列化很奇怪。它并不是真正为此设计的。我建议首先改变这种方法,因为从长远来看,它会为您节省很多痛苦。

您是否考虑过使用 DataOutputStream 代替?这是一种更加标准的方法,其优点是编写字符串和其他数据类型所需的所有实用方法都已经可供您使用......

I find it quite odd that you are using a BitSet for serialization. It's not really designed for this. I'd suggest altering this approach first of all, as it will save you a lot of pain in the long run.

Have you considered using a DataOutputStream instead? This is a much more standard approach and has the advantages that all the utility methods needed for writing Strings and other data types are already there for you to use....

┾廆蒐ゝ 2024-10-17 03:50:21

看来您的代码不会执行您想要的操作。首先,您的数组将始终充满 \0 (nul) 字符。也许您的意图要简单得多。

我怀疑没有简单的方法可以完成您想要的操作,因为 BitSet 并不用于存储文本。也许您的意图

private static byte[] string_to_bytes(String text) {
    return text.getBytes();
}

通常是文件包含字节而不是位。

It appears to be that your code won't do what you want. For a start, your array will always be full of \0 (nul) characters. Perhaps what you intended is much simpler.

I suspect there is no simple way to do what you want because BitSet isn't intended to store text. Perhaps you intended

private static byte[] string_to_bytes(String text) {
    return text.getBytes();
}

Normally files contain bytes rather than bits.

秋日私语 2024-10-17 03:50:21

你的数据字节对齐了吗?您可以使用 ByteBuffer

据我所知,java 不提供位级 I/O 的函数或类。

Is your data byte aligned? You could use a ByteBuffer.

As far as I know java does not provide functions or classes for bit level I/O.

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