001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.text;
034:
035: import java.io.*;
036:
037: public final class Hex {
038: private Hex() {
039: // Do nothing
040: }
041:
042: public static void print(PrintStream out, byte[] bytes) {
043: for (byte b : bytes) {
044: print(out, b);
045: }
046: }
047:
048: public static void print(PrintWriter out, byte[] bytes) {
049: for (byte b : bytes) {
050: print(out, b);
051: }
052: }
053:
054: public static void print(PrintStream out, byte b) {
055: int highBits = (b & 0xF0) >> 4;
056: int lowBits = (b & 0x0F);
057:
058: print(out, highBits);
059: print(out, lowBits);
060: }
061:
062: public static void print(PrintWriter out, byte b) {
063: int highBits = (b & 0xF0) >> 4;
064: int lowBits = (b & 0x0F);
065:
066: print(out, highBits);
067: print(out, lowBits);
068: }
069:
070: public static void print(PrintStream out, int n) {
071: out.print(toHexChar(n));
072: }
073:
074: public static void print(PrintWriter out, int n) {
075: out.print(toHexChar(n));
076: }
077:
078: public static String toString(byte[] bytes) {
079: StringBuffer result = new StringBuffer();
080: toString(bytes, result);
081: return result.toString();
082: }
083:
084: private static void toString(byte[] bytes, StringBuffer buffer) {
085: for (byte b : bytes) {
086: toString(b, buffer);
087: }
088: }
089:
090: public static void toString(byte b, StringBuffer buffer) {
091: int highBits = (b & 0xF0) >> 4;
092: int lowBits = (b & 0x0F);
093:
094: toString(highBits, buffer);
095: toString(lowBits, buffer);
096: }
097:
098: private static void toString(int n, StringBuffer buffer) {
099: buffer.append(toHexChar(n));
100: }
101:
102: public static String toHexChar(int n) {
103: return Integer.toHexString(n).toUpperCase();
104: }
105: }
|