KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ice > text > HexNumberFormat


1 /*
2 ** Copyright (c) 1997 by Timothy Gerard Endres
3 **
4 ** This program is free software.
5 **
6 ** You may redistribute it and/or modify it under the terms of the GNU
7 ** General Public License as published by the Free Software Foundation.
8 ** Version 2 of the license should be included with this distribution in
9 ** the file LICENSE, as well as License.html. If the license is not
10 ** included with this distribution, you may find a copy at the FSF web
11 ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
12 ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
13 **
14 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
15 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
16 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
17 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
18 ** REDISTRIBUTION OF THIS SOFTWARE.
19 **
20 */

21
22 package com.ice.text;
23
24 import java.lang.*;
25 import java.text.*;
26 import java.util.*;
27
28 /**
29  * The HexNumberFormat class implements the code necessary
30  * to format and parse Hexidecimal integer numbers.
31  *
32  * @version $Revision: 1.1 $
33  * @author Timothy Gerard Endres, <a HREF="mailto:time@ice.com">time@ice.com</a>.
34  * @see java.text.NumberFormat
35  */

36
37 public class
38 HexNumberFormat extends Format
39     {
40     static public final String JavaDoc RCS_ID = "$Id: HexNumberFormat.java,v 1.1 2002/03/14 09:55:00 deniger Exp $";
41     static public final String JavaDoc RCS_REV = "$Revision: 1.1 $";
42
43     private static char[] lowChars;
44     private static char[] uprChars;
45
46     private int count;
47     private String JavaDoc pattern;
48     private static char[] hexChars;
49
50     static
51         {
52         HexNumberFormat.lowChars = new char[20];
53         HexNumberFormat.uprChars = new char[20];
54         
55         HexNumberFormat.uprChars[0] = HexNumberFormat.lowChars[0] = '0';
56         HexNumberFormat.uprChars[1] = HexNumberFormat.lowChars[1] = '1';
57         HexNumberFormat.uprChars[2] = HexNumberFormat.lowChars[2] = '2';
58         HexNumberFormat.uprChars[3] = HexNumberFormat.lowChars[3] = '3';
59         HexNumberFormat.uprChars[4] = HexNumberFormat.lowChars[4] = '4';
60         HexNumberFormat.uprChars[5] = HexNumberFormat.lowChars[5] = '5';
61         HexNumberFormat.uprChars[6] = HexNumberFormat.lowChars[6] = '6';
62         HexNumberFormat.uprChars[7] = HexNumberFormat.lowChars[7] = '7';
63         HexNumberFormat.uprChars[8] = HexNumberFormat.lowChars[8] = '8';
64         HexNumberFormat.uprChars[9] = HexNumberFormat.lowChars[9] = '9';
65         HexNumberFormat.uprChars[10] = 'A'; HexNumberFormat.lowChars[10] = 'a';
66         HexNumberFormat.uprChars[11] = 'B'; HexNumberFormat.lowChars[11] = 'b';
67         HexNumberFormat.uprChars[12] = 'C'; HexNumberFormat.lowChars[12] = 'c';
68         HexNumberFormat.uprChars[13] = 'D'; HexNumberFormat.lowChars[13] = 'd';
69         HexNumberFormat.uprChars[14] = 'E'; HexNumberFormat.lowChars[14] = 'e';
70         HexNumberFormat.uprChars[15] = 'F'; HexNumberFormat.lowChars[15] = 'f';
71         }
72
73     static public final HexNumberFormat
74     getInstance()
75         {
76         return new HexNumberFormat( "XXXXXXXX" );
77         }
78
79     public
80     HexNumberFormat( String JavaDoc pattern )
81         {
82         super();
83         this.pattern = pattern;
84         this.count = pattern.length();
85         this.hexChars =
86             ( pattern.charAt(0) == 'X'
87                 ? HexNumberFormat.uprChars
88                 : HexNumberFormat.lowChars );
89         }
90
91     public String JavaDoc
92     format( int hexNum )
93         throws IllegalArgumentException JavaDoc
94         {
95         FieldPosition pos = new FieldPosition(0);
96         StringBuffer JavaDoc hexBuf = new StringBuffer JavaDoc(8);
97
98         this.format( new Integer JavaDoc( hexNum ), hexBuf, pos );
99
100         return hexBuf.toString();
101         }
102
103     public StringBuffer JavaDoc
104     format( Object JavaDoc hexInt, StringBuffer JavaDoc appendTo, FieldPosition fieldPos )
105         throws IllegalArgumentException JavaDoc
106         {
107         char[] hexBuf = new char[16];
108         
109         int end = fieldPos.getEndIndex();
110         int beg = fieldPos.getBeginIndex();
111
112         int hexNum = ((Integer JavaDoc) hexInt).intValue();
113
114         for ( int i = 7 ; i >= 0 ; --i )
115             {
116             hexBuf[i] = this.hexChars[ (hexNum & 0x0F) ];
117             hexNum = hexNum >> 4;
118             }
119
120         for ( int i = (8 - this.count) ; i < 8 ; ++i )
121             {
122             appendTo.append( hexBuf[i] );
123             }
124
125         return appendTo;
126         }
127
128     public int
129     parse( String JavaDoc source )
130         throws ParseException
131         {
132         throw new ParseException( "unimplemented!", 0 );
133         }
134
135     public Object JavaDoc
136     parseObject( String JavaDoc source, ParsePosition pos )
137         {
138         return null;
139         }
140
141     }
142
143
Popular Tags