source = Byte.parseByte(display);

解决方案 »

  1.   

    to:AlexJava2(+黑色契约+) 不能用呀,parseByte返回的是一个byte,可source是byte[]呀
      

  2.   

    没有改变啊,你是不是直接System.out.println(source);了?不能这样显示,这样显示的是source的地址,它的内容不变。
      

  3.   

    可能大家误会我的意思了
    我现在是这样的情况:首先我通过加密算法将一个byte[]对象进行加密后,得到source,我想将source这个byte[]对象转换为String(这样可以一个乱妈串)后,我把这个乱码String保存;下次我要读内容的时候,将这个乱码String读出来,用String.getBytes方法得到他的byte[],在进行解密,
    可现在的问题是,我将乱码String变成byte[]后,与原来的数据不一样了,就是这样一个问题。
    简单来说就是byte[] 1 -->String1---->byte[]2
    经过这样的过程后:
    byte[]2 != byte[]1怎么解决这个问题!
      

  4.   

    byte ->string
    byte.toString();
    String->byte
    String.valueOf();^_^
      

  5.   

    TO: gong1(轻松编程) 
    我现在是进行byte[]  to  String   String to byte[]的转换哦
      

  6.   

    我查了下JDK1.4的文档:
    public String(byte[] bytes)
    Constructs a new String by decoding the specified array of bytes using the platform's default charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array. //注意这句话:新的String长度是由字符集来负责的,因此长度就可能不和原来的字节数组一样了,所以再转回来的时候就不等于原来的了.:)
    这样转是行不通的~
      

  7.   

    The behavior of this constructor when the given bytes are not valid in the default charset is unspecified. The CharsetDecoder class should be used when more control over the decoding process is required. 当构建器接收的bytes参数在默认字符集中不是有效的话,那么就被看作是未指明的.
      

  8.   

    未指明的话,新的String长度就可能不和原来的字节数组长度一样了~~简单的说:
    Bytes[](length = 100)  ->  String(length = 200)  ->  Bytes[](length = 200)
      

  9.   

    我好像没有看到java中有和c++中用构造函数复制!
      

  10.   

    头晕 哪里有这样转换的 好象中国讲英语 所谓的chiglish一样 hh
      

  11.   

    String string1 = new String(source);
      

  12.   

    给两个转换函数好了。byte->int:
    public static int byteArr2Int(byte[] arrB)
    {
        if (arrB==null || arrB.length!=4) 
        {
         return 0;
        }
        int i = (arrB[0]<<24) + (arrB[1]<<16) + (arrB[2]<<8) + arrB[3];
        return i;
    }
    把int型转换成String型。
    int->byte:
    先把int型转换成String型。
    public static byte[] int2ByteArr(int i)
    {
        byte[] arrB = new byte[4];
        arrB[0] = (byte)(i>>24);
        arrB[1] = (byte)(i>>16);
        arrB[2] = (byte)(i>>8);
        arrB[3] = (byte)i;
        return arrB;
    }byte类型是有符号的。min=-128 ,max=127