Convert Hex to Byte with big string

I tried the different ways to convert hex to byte, there are four methods in the code, three of them I comment it out, only one there is no error when I run it, but I confused that when I repeated to run the code, it gave me the different result (should be generate "byte").

There is a question is when I use "method1", it gave me the result (byte), but once I changed to "method2", it will not generate the result, I don't know why. I thought it should generate same result, when I have same string.

public class Convert {
/**
 * @param args
 * @throws IOException 
 */

// String everything;

public static void main(String[] args) throws IOException {
    //String everything;
    // TODO Auto-generated method stub
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader("C:\\TEMP1\\Doctor.txt"));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            line = br.readLine();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        while (line != null) {
            sb.append(line);
            sb.append('\n');
            line = br.readLine();
        }

        //*********Method 1****************
            //String r="1ee079746828d7c6f9af46f93c1ef2555ff4b14b2378ad53a258d18dc6a8363fb57f3448783833722bd9ef291ba53153afca31a96de404755e78f68b76fd5a77e4be3b984ea25244842e92a8ed40da1f1a588fb3da26b8bc21d74cd8476534f26ee454df086567c4d7cf3334f794cede41a9b051a5c393a35584afcf";
            //byte[] b = new BigInteger(r,16).toByteArray();
            //System.out.println("Byte for public key: "+b); 



        //*********Method 2****************
            //String r2 = sb.toString();
            //System.out.println("Doctor contect file: "+r2);
            //byte[] b = new BigInteger(r2,16).toByteArray();
            //System.out.println("Byte for public key: "+b); 

        //********Method 3*****************

            String r="1ee079746828d7c6f9af46f93c1ef2555ff4b14b2378ad53a258d18dc6a8363fb57f3448783833722bd9ef291ba53153afca31a96de404755e78f68b76fd5a77e4be3b984ea25244842e92a8ed40da1f1a588fb3da26b8bc21d74cd8476534f26ee454df086567c4d7cf3334f794cede41a9b051a5c393a35584afcf";  
            int len = r.length();
            byte[] data = new byte[len / 2];
            for (int i = 0; i < len; i += 2) {
                data[i / 2] = (byte) ((Character.digit(r.charAt(i), 16) << 4)
                                 + Character.digit(r.charAt(i+1), 16));
                System.out.println(data);
        }


        //********Method4******************
        /*
            String r2 = sb.toString();
            int len = r2.length();
            byte[] data = new byte[len / 2];
            for (int i = 0; i < len; i += 2) {
                data[i / 2] = (byte) ((Character.digit(r2.charAt(i), 16) << 4)
                                     + Character.digit(r2.charAt(i+1), 16));
                System.out.println(data);
            }
        */






        //String r=everything;

       // String r="1ee079746828d7c6f9af46f93c1ef2555ff4b14b2378ad53a258d18dc6a8363fb57f3448783833722bd9ef291ba53153afca31a96de404755e78f68b76fd5a77e4be3b984ea25244842e92a8ed40da1f1a588fb3da26b8bc21d74cd8476534f26ee454df086567c4d7cf3334f794cede41a9b051a5c393a35584afcf";
       // double convert=Double.parseDouble(r);
       //  long convert=(long)(Integer.parseInt(r,32)&0xFF);
       // byte convert=Byte.parseByte(r,32);
       // byte convert=Integer.parseInt(everything,16);
      // System.out.println("Byte for public key: "+convert);

    } finally {
        br.close();
    }

}

}

Jon Skeet
people
quotationmark

You're printing the result of calling toString on a byte[]. That's not going to give you what you want.

For diagnostic purposes, use System.out.println(Arrays.toString(data)). And do that at the end of the loop rather than within it:

for (int i = 0; i < len; i += 2) {
   ...
}
System.out.println(Arrays.toString(data));

There are plenty of alternative approaches to parsing a hex string, mind you. I don't personally like the idea of using an XML-focused API (as recommended in the question comments) when you're not dealing with XML, but it would certainly work - and any number of third party APIs have hex conversion routines.

EDIT: As noted in comments, I believe your hex conversion code is also broken at the moment - but that should probably be fixed by using a prebuilt one from elsewhere. The main purpose of this answer was to explain why you're getting results such as "[B@40a0dcd9". Once you can see the data, you can verify it.

people

See more on this question at Stackoverflow