将字符串转换成重位presentation一个字节 [英] Converting a String representation of bits to a byte
问题描述
我刚开始学习有关文件COM pression和我碰到一个有点路障。我有一个应用程序,将连接codeA字符串,如计划作为一个COM pressed二进制重新presentation 010100111111011000
(注意,这是还是存储为一个字符串)。
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
现在我需要用写这文件系统中的的FileOutputStream
,我遇到的问题是,我怎么能转换成字符串010100111111011000发送字节[]
/ 字节
s到写入文件系统的FileOutputStream
?
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.
推荐答案
这是介绍位移位运算符:
An introduction to bit-shift operators:
首先,我们有左移位运算符, X<< ñ
。这将在所有位移位X
按向左移动n个
位,零填充新位:
First, we have the left-shift operator, x << n
. This will shift all the bits in x
left by n
bits, filling the new bits with zero:
1111 1111
<< 3: 1111 1000
接下来,我们有签署右移位运算符, X&GT;&GT; ñ
。这种转变在所有位X
右移n,复制符号位到新的位:
Next, we have the signed right-shift operator, x >> n
. This shifts all the bits in x
right by n, copying the sign bit into the new bits:
1111 1111
>> 3: 1111 1111
1000 0000
>> 3: 1111 0000
0111 1111
>> 3: 0000 1111
最后,我们有填零右移位运算符, X&GT;&GT;&GT; ñ
。这种转变在所有位X
向右 N
位,充新位零:
Finally, we have the zero-fill right-shift operator, x >>> n
. This shifts all bits in x
right by n
bits, filling the new bits with zero:
1111 1111
>>> 3: 0001 1111
您也可以找到有用的按位或操作, X |是
。这在每一个位置上的位比较了 X
和是
,设置新号码的位上,如果它是在任 X
或是
,否则断:
You may also find useful the bitwise-or operator, x | y
. This compares the bits in each position in x
and y
, setting the new number's bit on if it was on in either x
or y
, off otherwise:
1010 0101
| 1010 1010
---------
1010 1111
您应该只需要为手头的问题previous运营商,但对完整起见,这里是最后两个:
You should only need the previous operators for the problem at hand, but for the sake of completeness, here are the last two:
按位与运算符, X'放大器;是
设置输出的比特一当且仅当该位是在这两个 X
和是
:
The bitwise-and operator, x & y
sets the bits in the output to one if and only if the bit is on in both x
and y
:
1010 0101
& 1010 1010
---------
1010 0000
按位异或运算符, X ^是
设置输出位之一,如果该位是在一个数字或其他,但不能两者都:
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:
1010 0101
^ 1010 1010
---------
0000 1111
现在,手头这些应用的情况:
Now, applying these to the situation at hand:
您将需要使用比特移位运算符来添加和处理位。根据它们的字符串重新presentations在右侧开始设定位与移位他们。继续,直到你打一个字节的结束,然后移动到下一个字节。假设我们要创建一个字节再presentation1100 1010:
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":
Our byte Target
--------- --------
0000 0000
1100 1010
0000 0001 ^
1100 1010
0000 0011 ^
1100 1010
0000 0110 ^
1100 1010
0000 1100 ^
1100 1010
0001 1001 ^
1100 1010
0011 0010 ^
1100 1010
0110 0101 ^
1100 1010
1100 1010 ^
我会的,当然,它留给你这适用于你的工作。
I will, of course, leave it to you to apply this to your work.
这篇关于将字符串转换成重位presentation一个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!