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!
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));
}
}
--
Knute Johnson
email s/nospam/knute/
--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDem
>
>Good, but... i need those extra leading 0s! How to get back them?!
see http://mindprod.com/jgloss/hex.html
--
Roedy Green, Canadian Mind Products
The Java Glossary, http://mindprod.com
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();
> 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));
> }
> }
>
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....