Velocity Reviews > Java > Re: Simple hex formatting question

# Re: Simple hex formatting question

xarax
Guest
Posts: n/a

 08-27-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Gil Pius) wrote in message news:<(E-Mail Removed). com>...
> 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.

Steve Horsley
Guest
Posts: n/a

 08-27-2003
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

Phil...
Guest
Posts: n/a

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

"Douwe" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> (E-Mail Removed) (xarax) wrote in message

news:<(E-Mail Removed) om>...
> > (E-Mail Removed) (Gil Pius) wrote in message

news:<(E-Mail Removed). com>...
> > > 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
> }

Roedy Green
Guest
Posts: n/a

 08-27-2003
On 27 Aug 2003 06:11:04 -0700, (E-Mail Removed) (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

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

Douwe
Guest
Posts: n/a

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

> news:<(E-Mail Removed) om>...
> > > (E-Mail Removed) (Gil Pius) wrote in message

> news:<(E-Mail Removed). com>...
> > > > 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

Jon A. Cruz
Guest
Posts: n/a

 08-29-2003
xarax wrote:
> (E-Mail Removed) (Gil Pius) wrote in message news:<(E-Mail Removed). com>...
>
>>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);