将字节转换为字符串然后将字符串转换为字节但不相等

时间:2014-08-19 17:53:37

标签: java arrays string byte

我有一个字节数组,我也有2个方法,一个是HexEncode(将一个字节数组转换为十六进制字符串)另一个是HexDecode(将十六进制字符串转换为字节数组)

首先我打印字节数组,转换字符串和hexEncode 然后我编码,然后再解码这个, 打印时,字符串和hexEncodes都是相同的 但字节数组不一样。 这个问题是什么?

如何以两个字节数组的方式对其进行编码和解码 和其他人一样吗?

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;


public class MainClass {

    public static void main ( String args[]) throws NoSuchAlgorithmException{

        SecureRandom prng = SecureRandom.getInstance("SHA1PRNG");
        SecureRandom r = new SecureRandom();
        BigInteger p = new BigInteger(1024 / 2, 100, r);
        MessageDigest sha = MessageDigest.getInstance("SHA-1");
        byte[] result =  sha.digest(p.toByteArray());
        //now I have a random byte array :)


        System.out.println(result);
        System.out.println(hexEncode(result));
        String temp1 = new String(result);
        System.out.println(temp1);

        byte[] after = hexStringToByteArray(hexEncode(result));
        System.out.println(after );
        System.out.println(hexEncode(after));
        String temp2 = new String(after);
        System.out.println(temp2);

        if ( Arrays.equals(result, after) ){
            System.out.println("OK");           
        }
        else{
            System.out.println("Problem");
        }



    }
    private static  String hexEncode(byte[] aInput){
        StringBuilder result = new StringBuilder();
        char[] digits = {'0', '1', '2', '3', '4','5','6','7','8','9','a','b','c','d','e','f'};
        for (int idx = 0; idx < aInput.length; ++idx) {
            byte b = aInput[idx];
            result.append(digits[ (b&0xf0) >> 4 ]);
            result.append(digits[ b&0x0f]);
        }
        return result.toString();
    }

    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                                 + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }
}

示例输出是:

[B@29453f44
66384c6457e27cbfed4a45c080235f52e50e88a5
f8LdWâ|¿íJEÀ€#_R別
[B@5cad8086
66384c6457e27cbfed4a45c080235f52e50e88a5
f8LdWâ|¿íJEÀ€#_R別
OK

1 个答案:

答案 0 :(得分:0)

使用 Arrays.equals(byte[] b1, byte[] b2) 代替等于:

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;


public class MainClass {

    public static void main ( String args[]) throws NoSuchAlgorithmException{

        SecureRandom prng = SecureRandom.getInstance("SHA1PRNG");
        SecureRandom r = new SecureRandom();
        BigInteger p = new BigInteger(1024 / 2, 100, r);
        MessageDigest sha = MessageDigest.getInstance("SHA-1");
        byte[] result =  sha.digest(p.toByteArray());
        //now I have a random byte array :)


        System.out.println(result);
        System.out.println(hexEncode(result));
        String temp1 = new String(result);
        System.out.println(temp1);

        byte[] after = hexStringToByteArray(hexEncode(result));
        System.out.println(after );
        System.out.println(hexEncode(after));
        String temp2 = new String(after);
        System.out.println(temp2);

        if ( Arrays.equals(after, result)){
            System.out.println("OK");           
        }
        else{
            System.out.println("Problem");
        }



    }
    private static  String hexEncode(byte[] aInput){
        StringBuilder result = new StringBuilder();
        char[] digits = {'0', '1', '2', '3', '4','5','6','7','8','9','a','b','c','d','e','f'};
        for (int idx = 0; idx < aInput.length; ++idx) {
            byte b = aInput[idx];
            result.append(digits[ (b&0xf0) >> 4 ]);
            result.append(digits[ b&0x0f]);
        }
        return result.toString();
    }

    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                                 + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }
}