Re: Simple hex formatting question

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

1. xaraxGuest

(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

2. Steve HorsleyGuest

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

3. 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
4. Roedy GreenGuest

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

"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
6. Jon A. CruzGuest

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