Pads the string with 0x and zeros on the left until it has the requested size. : String Pad « Data Type « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Data Type » String PadScreenshots 
Pads the string with 0x and zeros on the left until it has the requested size.
   

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//package org.ancora.SharedLibrary;

/**
 * Methods for bit manipulation.
 *
 @author Joao Bispo
 */
public class Util{
     private static final String ZERO = "0";
     private static final String HEX_PREFIX = "0x";
    /**
    * Pads the string with 0x and zeros on the left until it has the
    * requested size.
    *
    @param hexNumber
    @param size
    @return
    */
   public static String padHexString(String hexNumber, int size) {
       int stringSize = hexNumber.length();

       if(stringSize >= size) {
           return hexNumber;
       }

       int numZeros = size - stringSize;
       StringBuilder builder = new StringBuilder(numZeros+HEX_PREFIX.length());
       builder.append(HEX_PREFIX);
       for(int i=0; i<numZeros; i++) {
           builder.append(ZERO);
       }

       return builder.toString() + hexNumber;
   }
}

   
    
    
  
Related examples in the same category
1.Left pad a String with a specified String.
2.Left pad a String with a specified character.
3.Left pad a String with spaces (' ').
4.Right pad a String with a specified String.
5.Right pad a String with a specified character.
6.Right pad a String with spaces (' ').
7.Padding and triming strings
8.Pad string
9.Padded String
10.Set string length, padding with character if the shorter, or truncating if longer
11.Returns the quoted version of the string using the quotechar argument.
12.Return a string padded with the given string for the given count.
13.Left pad the given text parameter
14.Right pad the given text parameter
15.Left padding
16.Right padding
17.Pads the string with zeros on the left until it has the requested size.
18.Right, left justify the string
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.