string转为int然后取得int的值,然后与运算,再转为2进制,再转回string

解决方案 »

  1.   

    抽空写了程序:public class and
    {
    public static void main(String[] args)
    {
    String s1 = new String("101011");//43
    String s2 = new String("000101");//5
    String s3 = new String("101001");//41

    int i1 = getint(s1);
    int i2 = getint(s2);
    int i3 = getint(s3);

    String s = new String(""); try
    {
    s = String.valueOf((i1&i2&i3));
    }
    catch(Exception e)
    {
    System.err.println(e);
    }

    System.out.println("s: " + s);
    }

    public static int getint(String s)
    {
    int i = 0;
    int lengths = s.length();
    int p = 0;
    int si = 0;
    String sc = new String("");

    for(int j = 0; j < lengths; j++)
    {
    sc = String.valueOf(s.charAt(j));
    si = Integer.parseInt(sc);

    p = power(2,(lengths - j - 1));
    i += p*si;
    }
    return i;
    }

    public static int power(int a, int p)
    {
    int res = 1; for(int i = 0; i < p; i++)
    res*=a;

    return res;
    }

    }