Mega Code Archive

 
Categories / Java / Data Type
 

Pads the string with 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 zeros on the left until it has the requested size.       *       * @param binaryNumber       * @param size       * @return       */      public static String padBinaryString(String binaryNumber, int size) {          int stringSize = binaryNumber.length();          if(stringSize >= size) {              return binaryNumber;          }          int numZeros = size - stringSize;          StringBuilder builder = new StringBuilder(numZeros);          for(int i=0; i<numZeros; i++) {              builder.append(ZERO);          }          return builder.toString() + binaryNumber;      } }