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.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Data Type » String Pad 




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.