1   /* $RCSfile: IntegerUtil.java,v $
2    * $Revision: 1.3 $
3    * $Date: 2002/01/04 14:05:31 $
4    * $Author: uwe $
5    * $State: Exp $
6    *
7    * Created on July 12, 2001, 5:20 PM
8    *
9    * Copyright (C) 2001 Uwe Guenther <uwe@cscc.de>
10   *
11   * This file is part of the jhbci JCE-ServiceProvider. The jhbci JCE-
12   * ServiceProvider is a library, written in JavaTM, that should be 
13   * used in HBCI banking applications (clients and may be servers),
14   * to do cryptographic operations.
15   *
16   * The jhbci library is free software; you can redistribute it and/or
17   * modify it under the terms of the GNU Lesser General Public
18   * License as published by the Free Software Foundation; either
19   * version 2.1 of the License, or (at your option) any later version.
20   *
21   * The jhbci library is distributed in the hope that it will be useful,
22   * but WITHOUT ANY WARRANTY; without even the implied warranty of
23   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24   * Lesser General Public License for more details.
25   *
26   * You should have received a copy of the GNU Lesser General Public
27   * License along with this library; if not, write to the Free Software
28   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29   *
30   */
31  
32  package de.cscc.crypto.util;
33  
34  /** 
35   * IntegerUtil Class.
36   *
37   * Class that hold only static methods to convert ints and int arrays to
38   * Strings. These Strings holds the hex or binary representation of the
39   * converted ints or int arrays. This class can't be instance, because
40   * its constructor is private.
41   *
42   * @author  <a href=mailto:uwe@cscc.de>Uwe G&uuml;nther</a>
43   * @version $Revision: 1.3 $
44   */
45  public final class IntegerUtil {
46      
47      /** This class can't be instance, because its constructor is private. */
48      private IntegerUtil() {}
49  
50      /** 
51       * Converts byte to binary representation.
52       *
53       * @param in int that will be converted.
54       * @return String with hex representation of in.
55       */        
56      public static String toBin(int in){
57          
58          return toBinGeneric(in);
59      }
60      
61      /** 
62       * Converts int[] to binary representation.
63       *
64       * @param in int[] that will be converted.
65       * @return String with hex representation of in.
66       */        
67      public static String toBin(int[] in){
68          
69          StringBuffer tmp = new StringBuffer();
70          
71          for (int i = 0; i < in.length; i++) {
72              tmp.append(toBin(in[i]) + " ");
73          }
74            
75          return "[ " + tmp + "]";
76      
77      }
78      
79      /** 
80       * Converts int to binary representation.
81       *
82       * @param in int that will be converted
83       * @return String with hex representation of in
84       */        
85      private static String toBinGeneric(int in){
86  
87          StringBuffer tmp = new StringBuffer();
88          
89          for(int i = 0; i < 32; i++){
90              tmp.insert(0, (in >>> i) & 0x00000001);
91              
92          }
93          
94          return "0b" + tmp.toString();
95      }
96      
97      /** 
98       * Converts int to hex representation.
99       *
100      * @param in int that will be converted.
101      * @return String with hex representation of in.
102      */        
103     public static String toHex(int in) {
104         
105         return toHexGeneric(in);
106     }
107 
108     /**
109      * Converts int[] to hex representation.
110      *
111      * @param in int[] that will be converted.
112      * @return String with hex representation of in.
113      */        
114     public static String toHex(int[] in) {
115         
116         StringBuffer tmp = new StringBuffer();
117         
118         for (int i = 0; i < in.length; i++) {
119             tmp.append(toHexGeneric(in[i]) + " ");
120         }
121         
122         return "[ " + tmp.toString() + "]";
123     }
124     
125     /**
126      * Converts int to binary representation.
127      *
128      * @param in int that will be converted
129      * @return String with hex representation of in
130      */        
131     private static String toHexGeneric(int in){
132         return ByteUtil.toHex(toByteArray(in));     
133     }
134     
135     /**
136      * Converts int to byte[4]
137      *
138      * @param in int that should be converted to byte[4].
139      * @return byte array that holds the int in.
140      */    
141     public static byte[] toByteArray(int in) {
142         
143         byte[] temp = new byte[4];
144         
145         temp[0] = (byte)((in >>> 24) & 0xff);
146         temp[1] = (byte)((in >>> 16) & 0xff);
147         temp[2] = (byte)((in >>>  8) & 0xff);
148         temp[3] = (byte)((in >>>  0) & 0xff);
149         
150         return temp;
151     }
152 }
153