Hi
The method Integer.toHexString(int i) convert his argument to a string
of ASCII digits in hexadecimal (base 16) with no extra leading 0s.
Good, but... i need those extra leading 0s! How to get back them?!
Help!
Stefan Ram - 11 Jan 2008 19:15 GMT
>The method Integer.toHexString(int i) convert his argument to a string
>of ASCII digits in hexadecimal (base 16) with no extra leading 0s.
>Good, but... i need those extra leading 0s! How to get back them?!
"00" + Integer.toHexString( i )?
Giovanni D'Ascola - 11 Jan 2008 19:23 GMT
Stefan Ram ha scritto:
>> The method Integer.toHexString(int i) convert his argument to a string
>> of ASCII digits in hexadecimal (base 16) with no extra leading 0s.
>> Good, but... i need those extra leading 0s! How to get back them?!
>
> "00" + Integer.toHexString( i )?
Yes!
Knute Johnson - 11 Jan 2008 19:48 GMT
> Stefan Ram ha scritto:
>>> The method Integer.toHexString(int i) convert his argument to a
[quoted text clipped - 5 lines]
>>
> Yes!
If you want them to have the same number of characters you can use a
Formatter.
public class test6 {
public static void main(String[] args) {
System.out.println(String.format("%04x",0x2a));
}
}

Signature
Knute Johnson
email s/nospam/knute/
------->>>>>>http://www.NewsDem
Mark Space - 11 Jan 2008 21:30 GMT
> If you want them to have the same number of characters you can use a
> Formatter.
[quoted text clipped - 4 lines]
> }
> }
I was thinking a new type which remembers the number of digits in the
original and recreates it on output.
class MyHex {
int width;
int value;
public MyHex( String s ) {
// Count some hex digits, maybe the leading 0x should be removed?
width = s.length(); // TODO: Too simple by far....
value = Integer.parseInt( s, 16 );
}
public String toString() {
String formatString = "%0" + width + "x";
return String.format( formatString, value );
}
}
All this totally off the top of my head, I didn't check the APIs at all.
Also not it's syntax checked. This is just a general outline. And
MyHex should probably extend Number, but I didn't look into that either....
Roedy Green - 11 Jan 2008 21:11 GMT
On Fri, 11 Jan 2008 20:12:11 +0100, Giovanni D'Ascola
<giovanni.dascola@gmail.cm> wrote, quoted or indirectly quoted someone
who said :
>Good, but... i need those extra leading 0s! How to get back them?!
see http://mindprod.com/jgloss/hex.html

Signature
Roedy Green, Canadian Mind Products
The Java Glossary, http://mindprod.com
Lord Zoltar - 11 Jan 2008 21:29 GMT
On Jan 11, 2:12 pm, Giovanni D'Ascola <giovanni.dasc...@gmail.cm>
wrote:
> Hi
>
[quoted text clipped - 3 lines]
> Good, but... i need those extra leading 0s! How to get back them?!
> Help!
Sounds like you need a good string padding function
StringBuffer paddedString = new StringBuffer(str);
if (finalLength>0 && finalLength>str.length())
{
int padLength = finalLength - str.length();
char padding[] = new char[padLength];
Arrays.fill(padding, padChar);
if (padLeft)
paddedString.insert(0, padding);
else
paddedString.append(padding);
}
return paddedString.toString();