Re: Simple hex formatting question

Discussion in 'Java' started by xarax, Aug 27, 2003.

  1. xarax

    xarax Guest

    (Gil Pius) wrote in message news:<>...
    > 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.
    xarax, Aug 27, 2003
    #1
    1. Advertising

  2. On Wed, 27 Aug 2003 10:19:16 -0700, Douwe wrote:

    >
    > static final String TXT_HEX = "0123456789ABCDEF";
    > public String toHexString(int val) {
    > String result;
    > result += TXT_HEX[(val>>4) & 0xF];
    > result += TXT_HEX[(val) & 0xF];
    > return result
    > }


    NullPointerException I think - try this:

    public String toHexString(int val) {
    char digit1 = "0123456789ABCDEF".charAt(val & 15);
    char digit2 = "0123456789ABCDEF".charAt(val >> 4);
    String result = "" + digit1 + digit2;
    return result;
    }


    Steve
    Steve Horsley, Aug 27, 2003
    #2
    1. Advertising

  3. xarax

    Phil... Guest

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

    "Douwe" <> wrote in message
    news:...
    > (xarax) wrote in message

    news:<>...
    > > (Gil Pius) wrote in message

    news:<>...
    > > > 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.

    >
    > Although your version is really quick I think the next one looks nicer:
    >
    > static final String TXT_HEX = "0123456789ABCDEF";
    > public String toHexString(int val) {
    > String result;
    > result += TXT_HEX[(val>>4) & 0xF];
    > result += TXT_HEX[(val) & 0xF];
    > return result
    > }
    Phil..., Aug 27, 2003
    #3
  4. xarax

    Roedy Green Guest

    On 27 Aug 2003 06:11:04 -0700, (xarax) wrote or quoted
    :

    >> 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.


    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.
    Roedy Green, Aug 28, 2003
    #4
  5. xarax

    Douwe Guest

    "Phil..." <> wrote in message news:<gJ93b.276013$o%2.125768@sccrnsc02>...
    > don't you mean to have just "=" and not "+=" on the first line?
    >
    > "Douwe" <> wrote in message
    > news:...
    > > (xarax) wrote in message

    > news:<>...
    > > > (Gil Pius) wrote in message

    > news:<>...
    > > > > 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.

    > >
    > > Although your version is really quick I think the next one looks nicer:
    > >
    > > static final String TXT_HEX = "0123456789ABCDEF";
    > > public String toHexString(int val) {
    > > String result;
    > > result += TXT_HEX[(val>>4) & 0xF];
    > > result += TXT_HEX[(val) & 0xF];
    > > return result
    > > }



    oops :)
    Douwe, Aug 28, 2003
    #5
  6. xarax

    Jon A. Cruz Guest

    xarax wrote:
    > (Gil Pius) wrote in message news:<>...
    >
    >>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



    > 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.



    Or...

    String s = Integer.toHexString( 0x100 | (0x0ff & i) ).substring(1);
    Jon A. Cruz, Aug 29, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,181
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    461
    Juha Autero
    Aug 19, 2003
  3. jack
    Replies:
    4
    Views:
    583
  4. tim

    hex string to hex value

    tim, Nov 22, 2005, in forum: Python
    Replies:
    8
    Views:
    18,837
  5. tim
    Replies:
    2
    Views:
    1,554
    Dennis Lee Bieber
    Nov 23, 2005
Loading...

Share This Page