RandomString.java
  1 /*
  2  * stringbuilder.java
  3  *
  4  * Created on 13. Jänner 2009, 23:35
  5  * 
  6  * @author Alexander Scheidl
  7  *
  8  * utility class to create RandomStrings
  9  * 
 10  */
 11 
 12 package testrsa;
 13 
 14 import java.util.Random;
 15 
 16 
 17 
 18 public class RandomString {
 19     Random rand = new Random();
 20     
 21     /** Creates a new instance of RandomString */
 22     public RandomString() {
 23     }
 24   
 25   
 26       
 27     /**
 28      * !!!static methods only for testing purposes!!!
 29      * converts int to char
 30      *
 31      * @param temp int to convert
 32      * @return temp as char 
 33      */
 34       public static char createChar(int temp) {
 35           
 36      return (char)temp;
 37       }
 38      
 39      /**
 40      * !!!static methods only for testing purposes!!!
 41      * converts char to int
 42      *
 43      * @param temp char to convert
 44      * @return temp as int
 45      */      
 46      public static int createInt(char temp) {
 47  
 48      return (int)temp;
 49       }
 50     
 51      /**
 52      * !!!static methods only for testing purposes!!!
 53      * creation of the ascii table with comparision of Sting and Char
 54      *
 55      * prints out the table
 56      * 
 57      */
 58     public static void createAsciiTable(){
 59         for (int z = 0; z < 256; z++){
 60             char[] test3;
 61             test3 = new char [1];
 62             test3[0] = RandomString.createChar(z);
 63             String test2 = new String(test3);
 64             byte[] t3;
 65             t3 = test2.getBytes();
 66             int a2 = t3[0];
 67             int a3 = (a2 & 0x7F) + (a2 < 0 ? 128 : 0);
 68             System.out.println("mitChar: "+RandomString.createChar(z)+" "+RandomString.createInt(RandomString.createChar(z))+" mitString: "+test2+ " "+a3);
 69         }
 70         
 71      }
 72      
 73    
 74      /**
 75      * Creates a Random char
 76      *
 77      * 
 78      * @return temp char which represents the Ascii Code from 33 to 255
 79      */    
 80     public char createRandomChar() {
 81       
 82       int temp = this.rand.nextInt(256); //  limitation of Ascii Char 33 to 255
 83      
 84       
 85       if(temp < 33) // if < 33 call method recursive to avoid random number under 33
 86       {
 87         temp = createRandomChar();
 88          return (char)temp; 
 89       }
 90      
 91       else {
 92      // System.out.println(temp);
 93           return (char)temp;
 94     
 95       }
 96       
 97     }
 98 
 99     /**
100      * Creates a String
101      * 
102      * 
103      * @param length int  gives the number of char in the string
104      * @return sb String
105      */        
106     public String createRandomString(int length) {
107         StringBuffer sb = new StringBuffer();
108             for (int j = 0; j < length; j++) {
109                 sb.append(createRandomChar());
110             }
111            return sb.toString();
112     }
113     
114     
115     
116 }
117