Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2008

Tip: Looking for answers? Try searching our database.

Get back leading zeros!

Thread view: 
Giovanni D'Ascola - 11 Jan 2008 19:12 GMT
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();


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.