Source code: com/synchrona/util/Utilities.java
1 /*
2 **************************************************************************
3 ** $Header: /cvsroot/jred/jred/src/com/synchrona/util/Utilities.java,v 1.1.1.1 2000/07/05 04:41:53 mpatters Exp $
4 **
5 ** Copyright (C) 2000 Synchrona, Inc. All rights reserved.
6 **
7 ** This file is part of JRed, a 100% Java implementation of the IrDA
8 ** infrared communications protocols.
9 **
10 ** This file may be distributed under the terms of the Synchrona Public
11 ** License as defined by Synchrona, Inc. and appearing in the file
12 ** LICENSE included in the packaging of this file. The Synchrona Public
13 ** License is based on the Q Public License as defined by Troll Tech AS
14 ** of Norway; it differs only in its use of the courts of Florida, USA
15 ** rather than those of Oslo, Norway.
16 **************************************************************************
17 */
18 package com.synchrona.util;
19
20 public class Utilities {
21 public static String bytesToString(byte [] bytes, int offset, int length) {
22 if ( null == bytes ) {
23 return "(null)";
24 } else {
25 StringBuffer buffer = new StringBuffer();
26
27 for ( int i = offset; i < (offset + length); i++ ) {
28 buffer.append(byteToString(bytes[i]));
29 if ( Character.isLetterOrDigit((char) bytes[i]) ) {
30 buffer.append(" (" + (char) bytes[i] + ")");
31 }
32 if ( i < (bytes.length - 1) ) {
33 buffer.append(" ");
34 }
35 }
36 return new String(buffer);
37 }
38 }
39
40 public static String bytesToString(byte [] bytes) {
41 String strReturn = "(null)";
42
43 if ( (null != bytes) && (bytes.length > 0) ) {
44 strReturn = bytesToString(bytes, 0, bytes.length);
45 }
46 return strReturn;
47 }
48
49 public static String byteToString(byte b) {
50 byte hi_nibble = (byte) ((b & 0xF0) >> 4);
51 byte lo_nibble = (byte) (b & 0x0F);
52
53 return "0x" + nibbleToString(hi_nibble) + nibbleToString(lo_nibble);
54 //return Byte.toString(b);
55 }
56
57 private static String nibbleToString(byte nibble) {
58 switch ( nibble ) {
59 case 10: return "A";
60 case 11: return "B";
61 case 12: return "C";
62 case 13: return "D";
63 case 14: return "E";
64 case 15: return "F";
65 default: return Byte.toString(nibble);
66 }
67 }
68 }