发布网友 发布时间:2022-04-21 14:28
共2个回答
热心网友 时间:2023-07-14 13:49
public BitSet toBitSet(String s){
int len = s.length();
BitSet bs = new BitSet(len);
for(int i=0; i<len; i++) {
if( s.charAt(i) == '1' )
bs.set( i );
}
return bs;
}
public String toString(BitSet bs) {
int len = bs.length();
StringBuffer buf = new StringBuffer(len);
for(int i=0; i<len; i++)
buf.append( bs.get(i) ? '1' : '0' );
return buf.toString();
}
热心网友 时间:2023-07-14 13:49
import java.util.BitSet;
public class CCC
{
public static void main(String[] args)
{
String line0 = "01010001111";
BitSet bs = convertStr(line0);
p(new String(bs.toByteArray()));
p(pBitSet(bs));
}
public static BitSet convertStr(String s)
{
char[] ca = s.toCharArray();
BitSet bs = new BitSet(ca.length);
for (int i = 0; i < ca.length; i++)
{
char c = ca[i];
bs.set(i, c == '1');
}
return bs;
}
public static String pBitSet(BitSet bs)
{
String l = "";
for (int i = 0; i < bs.length(); i++)
{
if (bs.get(i))
{
l += '1';
}
else
{
l += '0';
}
}
return l;
}
public static void p(String s)
{
System.out.println(s);
}
}