12 Replies Latest reply: Jul 21, 2008 2:05 PM by 795426 RSS

    Convert an int value to a one byte hex

    807589
      How can I convert an int value to a one byte hex.

      I have tried the following but it is returning a two byte value.
      Integer.toHexString(intValue);
      I am trying to send a one byte hex value inside a message. What I do is construct the message, calculate an inValue and then try to convert it as a hex and add it into it. This message is finally sent as a stream of bytes.

      For example:

      The message structure is like this:
      Message: "\u0003"|Integer.toHexString(intValue)|BODY|"\u0004"
      I need to add this inValue as a hex. Moreover, I need it to be sent as a one byte hex value.


      When I use the above method to get the hex value I end up with a stream of bytes that looks like this:

      3
      36 <-- The first part of the hex value.
      36 <-- The second part of the hex value.
      xx
      xx
      ...
      4

      What I need instead is this:

      3
      36 <-- Only one hex value as one byte.
      xx
      xx
      ...
      4

      How can I achieve that?

      Thanks,

      B.D.
        • 1. Re: Convert an int value to a one byte hex
          DrClap
          int i=42;
          byte b = (byte) i;
          You seem to be getting the value of a number confused with its representation as a hexadecimal string.
          • 2. Re: Convert an int value to a one byte hex
            807589
            Integer.toHexString(intValue & 0x000000FF)
            You might have to pad the string if it return a Hex value smaller than 0x0F because it will only print a 1 character String.

            This will mask off any extra data that might be in the integer. It sounds to me as if it's getting inserted twice though. Double check your code to make sure you aren't calling a method twice or similar
            • 3. Re: Convert an int value to a one byte hex
              807589
              class Example {
                  public static void main(String[] args) {
                      test(63);
                      test(1024 + 63);
                      test(3);
                      test(0);
                      test(-1);
                  }
              
                  static void test(int x) {
                      byte b = (byte) x;
                      System.out.format("[%02X]%n", b);
                  }
              }
              • 4. Re: Convert an int value to a one byte hex
                807589
                I am not adding the value twice for sure. It is the getBytes() function that produces these two values to represent the String of a hex value.

                I tried this too:
                byte theByte = Integer.valueOf(Integer.toHexString(136)).intValue() - 256 = 88
                This one works.

                I subtracted 256 since the return value is greater than 127.

                However, if the return value of
                 Integer.toHexString(136)
                is 8A, then the
                Interger.ValueOf(aString)
                will not work.

                Is there a way to put this 8A into a byte instead of two bytes?
                • 5. Re: Convert an int value to a one byte hex
                  807589
                  BinaryDecimal wrote:
                  Is there a way to put this 8A into a byte instead of two bytes?
                  No. Have you looked at the java primitives before? [http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html]
                  The byte type is signed. So it doesn't have a range of 0 to 255, it has a range of -128 to 127. In other words, a single byte cannot represent any decimal value over 127, or 7F in hex. If you want 8A, you can't do it with a single byte.
                  • 6. Re: Convert an int value to a one byte hex
                    807589
                    BinaryDecimal wrote:
                    However, if the return value of
                     Integer.toHexString(136)
                    is 8A, then the
                    Interger.ValueOf(aString)
                    will not work.
                    That's because 8A is not a valid decimal number. You need to specify hex (radix 16). Then again, what's the point; you're just back where you started.
                    • 7. Re: Convert an int value to a one byte hex
                      795426
                      8A cannot be stored in a byte. It is too large. You don't need to do so anyways. Just use bitwise operations and mask off the part of the int you want displayed, then use PrintStream.printf() to format the number however you need.
                      int toDisplay = intVal & 0xFF;
                      System.out.printf("%02x",toDisplay);
                      • 8. Re: Convert an int value to a one byte hex
                        807589
                        0x8A can be stored in a byte, you just have to treat it as unsigned.

                        But I still have no idea what the OP wants. The question is very poorly put.

                        OP: "\u0003" is not one byte. It's two bytes.
                        • 9. Re: Convert an int value to a one byte hex
                          807589
                          Sorry if my question is not clear.

                          Let me re-state it again,

                          To be short, I need to send a CRC value represented in hex, as part of a message, to another application. The way I calculate the CRC returns me an int value. I cannot plug it in right away into the message since the other application treats this CRC as a one byte hex. So what I want to do is convert this int to a hex and add it to the message.

                          I hope this makes it easier.
                          • 10. Re: Convert an int value to a one byte hex
                            807589
                            Then reply #3 does what you want, right?
                            • 11. Re: Convert an int value to a one byte hex
                              807589
                              BinaryDecimal wrote:
                              So what I want to do is convert this int to a hex and add it to the message.
                              This does not make sense. Hexadecimal is a number base. You can convert the int into a string containing the hexadecimal representation of an int, but that's 8 characters (not bytes, unless you specify UTF-8/ASCII/other similar encoding). If you only need 4 bits out of the 32 in an int, then you could reduce it to one character. But it's not even clear if this is what you're after.
                              • 12. Re: Convert an int value to a one byte hex
                                807589
                                actually my question is why are you storing this message as a string instead of a byte[]? I mean the string is good for human readable, but it's an extra step to your byte output stream.