Simple hex formatting question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gil Pius

    Simple hex formatting question

    I have an int value which I know is between 0 and 255. I'd like to
    obtain a String containing two hex digits from it. I've looked at the
    Integer class and while it has a toHexString() method that method can
    produce as few as one digit. So if my int was 0 through 15 I'd get
    hex '0' thruough 'F'. I could get the length of the string and
    "prepend" a second zero in such cases, but I can't help but wonder if
    there's a simpler way to do this in Java that I'm missing. Any help
    would be greatly appreciated.

    Thanks,

    Gil
  • xarax

    #2
    Re: Simple hex formatting question

    gilpius@iname.c om (Gil Pius) wrote in message news:<3d815ea8. 0308270218.7280 bccc@posting.go ogle.com>...[color=blue]
    > I have an int value which I know is between 0 and 255. I'd like to
    > obtain a String containing two hex digits from it. I've looked at the
    > Integer class and while it has a toHexString() method that method can
    > produce as few as one digit. So if my int was 0 through 15 I'd get
    > hex '0' thruough 'F'. I could get the length of the string and
    > "prepend" a second zero in such cases, but I can't help but wonder if
    > there's a simpler way to do this in Java that I'm missing. Any help
    > would be greatly appreciated.
    >
    > Thanks,
    >
    > Gil[/color]

    Cross posted to comp.lang.java. programmer where it belongs.

    You could just have a static final String[] with 256
    elements ranging from "00", "01", ... , "fe", "ff" (i.e.,
    the sequence of hexadecimal representations ). Then simply
    index the array with (0xff & val) to select the String.

    Comment

    • Phil...

      #3
      Re: Simple hex formatting question

      don't you mean to have just "=" and not "+=" on the first line?

      "Douwe" <douwe@parkserv er.net> wrote in message
      news:df9b3384.0 308270919.3b883 1a3@posting.goo gle.com...[color=blue]
      > xarax@email.com (xarax) wrote in message[/color]
      news:<306d5c8f. 0308270511.e15f b8b@posting.goo gle.com>...[color=blue][color=green]
      > > gilpius@iname.c om (Gil Pius) wrote in message[/color][/color]
      news:<3d815ea8. 0308270218.7280 bccc@posting.go ogle.com>...[color=blue][color=green][color=darkred]
      > > > I have an int value which I know is between 0 and 255. I'd like to
      > > > obtain a String containing two hex digits from it. I've looked at the
      > > > Integer class and while it has a toHexString() method that method can
      > > > produce as few as one digit. So if my int was 0 through 15 I'd get
      > > > hex '0' thruough 'F'. I could get the length of the string and
      > > > "prepend" a second zero in such cases, but I can't help but wonder if
      > > > there's a simpler way to do this in Java that I'm missing. Any help
      > > > would be greatly appreciated.
      > > >
      > > > Thanks,
      > > >
      > > > Gil[/color]
      > >
      > > Cross posted to comp.lang.java. programmer where it belongs.
      > >
      > > You could just have a static final String[] with 256
      > > elements ranging from "00", "01", ... , "fe", "ff" (i.e.,
      > > the sequence of hexadecimal representations ). Then simply
      > > index the array with (0xff & val) to select the String.[/color]
      >
      > Although your version is really quick I think the next one looks nicer:
      >
      > static final String TXT_HEX = "0123456789ABCD EF";
      > public String toHexString(int val) {
      > String result;
      > result += TXT_HEX[(val>>4) & 0xF];
      > result += TXT_HEX[(val) & 0xF];
      > return result
      > }[/color]


      Comment

      • Roedy Green

        #4
        Re: Simple hex formatting question

        On 27 Aug 2003 06:11:04 -0700, xarax@email.com (xarax) wrote or quoted
        :
        [color=blue][color=green]
        >> I have an int value which I know is between 0 and 255. I'd like to
        >> obtain a String containing two hex digits from it.[/color][/color]

        see http://mindprod.com/jgloss/hex.html

        --
        Canadian Mind Products, Roedy Green.
        Coaching, problem solving, economical contract programming.
        See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

        Comment

        • Douwe

          #5
          Re: Simple hex formatting question

          "Phil..." <rynes@ieee.org > wrote in message news:<gJ93b.276 013$o%2.125768@ sccrnsc02>...[color=blue]
          > don't you mean to have just "=" and not "+=" on the first line?
          >
          > "Douwe" <douwe@parkserv er.net> wrote in message
          > news:df9b3384.0 308270919.3b883 1a3@posting.goo gle.com...[color=green]
          > > xarax@email.com (xarax) wrote in message[/color]
          > news:<306d5c8f. 0308270511.e15f b8b@posting.goo gle.com>...[color=green][color=darkred]
          > > > gilpius@iname.c om (Gil Pius) wrote in message[/color][/color]
          > news:<3d815ea8. 0308270218.7280 bccc@posting.go ogle.com>...[color=green][color=darkred]
          > > > > I have an int value which I know is between 0 and 255. I'd like to
          > > > > obtain a String containing two hex digits from it. I've looked at the
          > > > > Integer class and while it has a toHexString() method that method can
          > > > > produce as few as one digit. So if my int was 0 through 15 I'd get
          > > > > hex '0' thruough 'F'. I could get the length of the string and
          > > > > "prepend" a second zero in such cases, but I can't help but wonder if
          > > > > there's a simpler way to do this in Java that I'm missing. Any help
          > > > > would be greatly appreciated.
          > > > >
          > > > > Thanks,
          > > > >
          > > > > Gil
          > > >
          > > > Cross posted to comp.lang.java. programmer where it belongs.
          > > >
          > > > You could just have a static final String[] with 256
          > > > elements ranging from "00", "01", ... , "fe", "ff" (i.e.,
          > > > the sequence of hexadecimal representations ). Then simply
          > > > index the array with (0xff & val) to select the String.[/color]
          > >
          > > Although your version is really quick I think the next one looks nicer:
          > >
          > > static final String TXT_HEX = "0123456789ABCD EF";
          > > public String toHexString(int val) {
          > > String result;
          > > result += TXT_HEX[(val>>4) & 0xF];
          > > result += TXT_HEX[(val) & 0xF];
          > > return result
          > > }[/color][/color]


          oops :)

          Comment

          • Jon A. Cruz

            #6
            Re: Simple hex formatting question

            xarax wrote:[color=blue]
            > gilpius@iname.c om (Gil Pius) wrote in message news:<3d815ea8. 0308270218.7280 bccc@posting.go ogle.com>...
            >[color=green]
            >>I have an int value which I know is between 0 and 255. I'd like to
            >>obtain a String containing two hex digits from it. I've looked at the[/color][/color]

            [color=blue]
            > Cross posted to comp.lang.java. programmer where it belongs.
            >
            > You could just have a static final String[] with 256
            > elements ranging from "00", "01", ... , "fe", "ff" (i.e.,
            > the sequence of hexadecimal representations ). Then simply
            > index the array with (0xff & val) to select the String.[/color]


            Or...

            String s = Integer.toHexSt ring( 0x100 | (0x0ff & i) ).substring(1);

            Comment

            Working...