5 Replies Latest reply: Aug 9, 2010 1:44 PM by YoungWinston RSS

    Java Hex To Byte Array

    843789
      Hello,

      I have a problem. I`m using this code.
      The problem is that it`s not working like it should :S
      public static byte[] fromHexString(final String encoded) {
          if ((encoded.length() % 2) != 0)
              throw new IllegalArgumentException("Input string must contain an even number of characters");
      
          final byte result[] = new byte[encoded.length()/2];
          final char enc[] = encoded.toCharArray();
          for (int i = 0; i < enc.length; i += 2) {
              StringBuilder curr = new StringBuilder(2);
              curr.append(enc).append(enc[i + 1]);
      result[i/2] = (byte) Integer.parseInt(curr.toString(), 16);
      }
      return result;
      }

      If i do the following:
      String test = "61";
      System.out.println(me.fromHexString(test)); 
      It gives me result: [B@42e816

      Edited by: Iulian-Alexandru on 08.08.2010 23:49
        • 1. This Thread is now moved
          Darryl Burke
          Note: This thread was originally posted in the [Java Desktop Applications|http://forums.sun.com/forum.jspa?forumID=422] forum, but moved to this forum for closer topic alignment.
          • 2. Re: Java Hex To Byte Array
            Darryl Burke
            It gives me result: [B@42e816
            Yes, that's what an array's toString serves up. If you want to print the contents of the array, you need to iterate over the array and print each element. Or see the static toString methods of java.util.Arrays.

            db
            • 3. Re: Java Hex To Byte Array
              YoungWinston
              Iulian-Alexandru wrote:
              I have a problem. I`m using this code.
              You could have a look at [BigInteger(String, int)|http://download.oracle.com/javase/6/docs/api/java/math/BigInteger.html#BigInteger%28java.lang.String,%20int%29] and [BigInteger.toByteArray()|http://download.oracle.com/javase/6/docs/api/java/math/BigInteger.html#toByteArray%28%29]. I think you'll find they get you 90% of the way there in two lines of code (possibly 1).

              Winston
              • 4. Re: Java Hex To Byte Array
                798701
                Regarding BigInteger, leading zeroes will be a problem, and so will strings that would encode negative numbers. For example "0000" will come out as the array {0}, and "ABCD" will come out as {0, -85, -51}. It's easier to solve this one by hand, or use some 3rd party API ... IIRC apache commons had something for this.
                • 5. Re: Java Hex To Byte Array
                  YoungWinston
                  jsalonen wrote:
                  Regarding BigInteger, leading zeroes will be a problem, and so will strings that would encode negative numbers. For example "0000" will come out as the array {0}, and "ABCD" will come out as {0, -85, -51}. It's easier to solve this one by hand, or use some 3rd party API ... IIRC apache commons had something for this.
                  That's why I said 90% of the way there ;-)

                  The solution is still syntactically correct and padding with sufficient zero bytes or truncating a leading '0' shouldn't be too onerous.

                  Winston