001: /*
002: ** Copyright (c) 1997 by Timothy Gerard Endres
003: **
004: ** This program is free software.
005: **
006: ** You may redistribute it and/or modify it under the terms of the GNU
007: ** General Public License as published by the Free Software Foundation.
008: ** Version 2 of the license should be included with this distribution in
009: ** the file LICENSE, as well as License.html. If the license is not
010: ** included with this distribution, you may find a copy at the FSF web
011: ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
012: ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
013: **
014: ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
015: ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
016: ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
017: ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
018: ** REDISTRIBUTION OF THIS SOFTWARE.
019: **
020: */
021:
022: package com.ice.text;
023:
024: import java.lang.*;
025: import java.text.*;
026: import java.util.*;
027:
028: /**
029: * The HexNumberFormat class implements the code necessary
030: * to format and parse Hexidecimal integer numbers.
031: *
032: * @version $Revision: 1.1 $
033: * @author Timothy Gerard Endres, <a href="mailto:time@ice.com">time@ice.com</a>.
034: * @see java.text.NumberFormat
035: */
036:
037: public class HexNumberFormat extends Format {
038: static public final String RCS_ID = "$Id: HexNumberFormat.java,v 1.1 2002/03/14 09:55:00 deniger Exp $";
039: static public final String RCS_REV = "$Revision: 1.1 $";
040:
041: private static char[] lowChars;
042: private static char[] uprChars;
043:
044: private int count;
045: private String pattern;
046: private static char[] hexChars;
047:
048: static {
049: HexNumberFormat.lowChars = new char[20];
050: HexNumberFormat.uprChars = new char[20];
051:
052: HexNumberFormat.uprChars[0] = HexNumberFormat.lowChars[0] = '0';
053: HexNumberFormat.uprChars[1] = HexNumberFormat.lowChars[1] = '1';
054: HexNumberFormat.uprChars[2] = HexNumberFormat.lowChars[2] = '2';
055: HexNumberFormat.uprChars[3] = HexNumberFormat.lowChars[3] = '3';
056: HexNumberFormat.uprChars[4] = HexNumberFormat.lowChars[4] = '4';
057: HexNumberFormat.uprChars[5] = HexNumberFormat.lowChars[5] = '5';
058: HexNumberFormat.uprChars[6] = HexNumberFormat.lowChars[6] = '6';
059: HexNumberFormat.uprChars[7] = HexNumberFormat.lowChars[7] = '7';
060: HexNumberFormat.uprChars[8] = HexNumberFormat.lowChars[8] = '8';
061: HexNumberFormat.uprChars[9] = HexNumberFormat.lowChars[9] = '9';
062: HexNumberFormat.uprChars[10] = 'A';
063: HexNumberFormat.lowChars[10] = 'a';
064: HexNumberFormat.uprChars[11] = 'B';
065: HexNumberFormat.lowChars[11] = 'b';
066: HexNumberFormat.uprChars[12] = 'C';
067: HexNumberFormat.lowChars[12] = 'c';
068: HexNumberFormat.uprChars[13] = 'D';
069: HexNumberFormat.lowChars[13] = 'd';
070: HexNumberFormat.uprChars[14] = 'E';
071: HexNumberFormat.lowChars[14] = 'e';
072: HexNumberFormat.uprChars[15] = 'F';
073: HexNumberFormat.lowChars[15] = 'f';
074: }
075:
076: static public final HexNumberFormat getInstance() {
077: return new HexNumberFormat("XXXXXXXX");
078: }
079:
080: public HexNumberFormat(String pattern) {
081: super ();
082: this .pattern = pattern;
083: this .count = pattern.length();
084: this .hexChars = (pattern.charAt(0) == 'X' ? HexNumberFormat.uprChars
085: : HexNumberFormat.lowChars);
086: }
087:
088: public String format(int hexNum) throws IllegalArgumentException {
089: FieldPosition pos = new FieldPosition(0);
090: StringBuffer hexBuf = new StringBuffer(8);
091:
092: this .format(new Integer(hexNum), hexBuf, pos);
093:
094: return hexBuf.toString();
095: }
096:
097: public StringBuffer format(Object hexInt, StringBuffer appendTo,
098: FieldPosition fieldPos) throws IllegalArgumentException {
099: char[] hexBuf = new char[16];
100:
101: int end = fieldPos.getEndIndex();
102: int beg = fieldPos.getBeginIndex();
103:
104: int hexNum = ((Integer) hexInt).intValue();
105:
106: for (int i = 7; i >= 0; --i) {
107: hexBuf[i] = this .hexChars[(hexNum & 0x0F)];
108: hexNum = hexNum >> 4;
109: }
110:
111: for (int i = (8 - this .count); i < 8; ++i) {
112: appendTo.append(hexBuf[i]);
113: }
114:
115: return appendTo;
116: }
117:
118: public int parse(String source) throws ParseException {
119: throw new ParseException("unimplemented!", 0);
120: }
121:
122: public Object parseObject(String source, ParsePosition pos) {
123: return null;
124: }
125:
126: }
|