将位的字符串表示形式转换为字节
我刚刚开始学习文件压缩,但遇到了一些障碍。我有一个应用程序,它将诸如“program”之类的字符串编码为压缩二进制表示形式“010100111111011000”
(请注意,这仍然存储为字符串)。
Encoding
g 111
r 10
a 110
p 010
o 011
m 00
现在我需要使用 FileOutputStream 将其写入文件系统,我遇到的问题是,如何将字符串“010100111111011000”转换为 byte[] /byte
要使用 FileOutputStream
写入文件系统吗?
我以前从未使用过位/字节,所以我在这里陷入了死胡同。
I'm just beginning to learn about file compression and I've run into a bit of a roadblock. I have an application that will encode a string such as "program" as a compressed binary representation "010100111111011000"
(note this is still stored as a String).
Encoding
g 111
r 10
a 110
p 010
o 011
m 00
Now I need to write this to the file system using a FileOutputStream
, the problem I'm having is, how can I convert the string "010100111111011000" to a byte[]
/byte
s to be written to the file system with FileOutputStream
?
I've never worked with bits/bytes before so I'm kind of at a dead end here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
位移位运算符简介:
首先,我们有左移运算符,
x << n
。这会将x
中的所有位向左移动n
位,用零填充新位:接下来,我们有符号右移运算符
x > ;> n
。这会将 x 中的所有位右移 n,将符号位复制到新位中:最后,我们有零填充右移运算符 x >>>> 。 n。这会将
x
中的所有位右移n
位,用零填充新位:您可能还会发现按位或运算符
x | 很有用。 y。这会比较
x
和y
中每个位置的位,如果新数字的位在x
或中处于打开状态,则将其设置为打开y
,否则关闭:您应该只需要前面的运算符来解决手头的问题,但为了完整起见,这里是最后两个:
按位与运算符,
x &当且仅当该位在
将输出中的位设置为 1:x
和y
中均打开时,y按位异或运算符,
x ^ y
如果该位在一个数字或另一个数字中打开,但不是同时打开,则将输出位设置为 1:现在,将这些应用到当前的情况:
您将需要使用位移运算符来添加并操纵位。开始根据字符串表示在右侧设置位并将它们移过来。继续,直到到达一个字节的末尾,然后移至下一个字节。假设我们想要创建“1100 1010”的字节表示:
当然,我会留给您将其应用到您的工作中。
An introduction to bit-shift operators:
First, we have the left-shift operator,
x << n
. This will shift all the bits inx
left byn
bits, filling the new bits with zero:Next, we have the signed right-shift operator,
x >> n
. This shifts all the bits inx
right by n, copying the sign bit into the new bits:Finally, we have the zero-fill right-shift operator,
x >>> n
. This shifts all bits inx
right byn
bits, filling the new bits with zero:You may also find useful the bitwise-or operator,
x | y
. This compares the bits in each position inx
andy
, setting the new number's bit on if it was on in eitherx
ory
, off otherwise:You should only need the previous operators for the problem at hand, but for the sake of completeness, here are the last two:
The bitwise-and operator,
x & y
sets the bits in the output to one if and only if the bit is on in bothx
andy
:The bitwise-xor operator,
x ^ y
sets the output bits to one if the bit is on in one number or the other but not both:Now, applying these to the situation at hand:
You will need to use the bit-shift operators to add and manipulate bits. Start setting bits at the right side according to their string representations and shift them over. Continue until you hit the end of a byte, and then move to the next byte. Say we want to create a byte representation of "1100 1010":
I will, of course, leave it to you to apply this to your work.
将
String
切成8
的长度并调用 Byte#parseByte。如果将radix
设置为2
,它将把String
解析为二进制数。Chop your
String
up into lengths of8
and call Byte#parseByte. If you set theradix
to2
, it will parse theString
as a binary number.我想,您想将这些零和一作为二进制值写入文件中。因此,您可以每次迭代采用 8 个符号的字符串(String.substring() 或 smth),并使用 Byte(String) 构造函数创建字节。
这是我目前想到的最简单的解决方案。
如果我对这个问题的看法不正确,请详细说明。
I guess, you want to write these zeros and ones as binary values in a file. I so, you can iterate the string taking 8 signs everytime (String.substring() or smth) and create bytes with Byte(String) constructor.
It's the easiest solution that comes to my mind for now.
If i'm not right about the problem, tell more about it please.