3 Replies Latest reply: Aug 20, 2008 5:57 PM by 843785 RSS

    byte to hex

    843785
      1 byte = 2 hex

      Hex1Hex2

      I would like to get Hex1 as unencoded string
      and then Hex2 as unencoded string.

      How do I do that? When I search they said something about bitwise but I couldn't get it to works

      byte something= 0xAF
      byte b= (byte)(8 & something);
      gives 8 but not A, I need A and same thing for F

      even tried sb.append(String.format("%1X", something)); //didn't do anything
        • 1. Re: byte to hex
          843785
          A byte holds a value. Hex is a format. You don't really have to convert from a byte to hex.
          even tried sb.append(String.format("%1X", something)); //didn't do anything
          Well, that's basically how you'd do it (although you'd probably want to use "%02X").
          You of course would have to output the StringBuffer at some point.
          • 2. Re: byte to hex
            843785
            Convert your bytes to integers and use

            http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#toHexString(int)
            • 3. Re: byte to hex
              843785
              hah I got it, thanks guys

              for the second hex
                      StringBuilder sb = new StringBuilder();
                      String temp = sb.append(String.format("%02X", b)).toString();
                      return "" + temp.charAt(1);
              for the first hex
                      StringBuilder sb = new StringBuilder();
                      String temp= sb.append(String.format("%02X", b)).toString();
                      return "" + temp.charAt(0);