1 /* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.polo.pairing; 18 19 public class HexDump 20 { 21 private final static char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; 22 23 public static String dumpHexString(byte[] array) 24 { 25 return dumpHexString(array, 0, array.length); 26 } 27 28 public static String dumpHexString(byte[] array, int offset, int length) 29 { 30 StringBuilder result = new StringBuilder(); 31 32 byte[] line = new byte[16]; 33 int lineIndex = 0; 34 35 result.append("\n0x"); 36 result.append(toHexString(offset)); 37 38 for (int i = offset ; i < offset + length ; i++) 39 { 40 if (lineIndex == 16) 41 { 42 result.append(" "); 43 44 for (int j = 0 ; j < 16 ; j++) 45 { 46 if (line[j] > ' ' && line[j] < '~') 47 { 48 result.append(new String(line, j, 1)); 49 } 50 else 51 { 52 result.append("."); 53 } 54 } 55 56 result.append("\n0x"); 57 result.append(toHexString(i)); 58 lineIndex = 0; 59 } 60 61 byte b = array[i]; 62 result.append(" "); 63 result.append(HEX_DIGITS[(b >>> 4) & 0x0F]); 64 result.append(HEX_DIGITS[b & 0x0F]); 65 66 line[lineIndex++] = b; 67 } 68 69 if (lineIndex != 16) 70 { 71 int count = (16 - lineIndex) * 3; 72 count++; 73 for (int i = 0 ; i < count ; i++) 74 { 75 result.append(" "); 76 } 77 78 for (int i = 0 ; i < lineIndex ; i++) 79 { 80 if (line[i] > ' ' && line[i] < '~') 81 { 82 result.append(new String(line, i, 1)); 83 } 84 else 85 { 86 result.append("."); 87 } 88 } 89 } 90 91 return result.toString(); 92 } 93 94 public static String toHexString(byte b) 95 { 96 return toHexString(toByteArray(b)); 97 } 98 99 public static String toHexString(byte[] array) 100 { 101 return toHexString(array, 0, array.length); 102 } 103 104 public static String toHexString(byte[] array, int offset, int length) 105 { 106 char[] buf = new char[length * 2]; 107 108 int bufIndex = 0; 109 for (int i = offset ; i < offset + length; i++) 110 { 111 byte b = array[i]; 112 buf[bufIndex++] = HEX_DIGITS[(b >>> 4) & 0x0F]; 113 buf[bufIndex++] = HEX_DIGITS[b & 0x0F]; 114 } 115 116 return new String(buf); 117 } 118 119 public static String toHexString(int i) 120 { 121 return toHexString(toByteArray(i)); 122 } 123 124 public static byte[] toByteArray(byte b) 125 { 126 byte[] array = new byte[1]; 127 array[0] = b; 128 return array; 129 } 130 131 public static byte[] toByteArray(int i) 132 { 133 byte[] array = new byte[4]; 134 135 array[3] = (byte)(i & 0xFF); 136 array[2] = (byte)((i >> 8) & 0xFF); 137 array[1] = (byte)((i >> 16) & 0xFF); 138 array[0] = (byte)((i >> 24) & 0xFF); 139 140 return array; 141 } 142 143 private static int toByte(char c) 144 { 145 if (c >= '0' && c <= '9') return (c - '0'); 146 if (c >= 'A' && c <= 'F') return (c - 'A' + 10); 147 if (c >= 'a' && c <= 'f') return (c - 'a' + 10); 148 149 throw new RuntimeException ("Invalid hex char '" + c + "'"); 150 } 151 152 public static byte[] hexStringToByteArray(String hexString) 153 { 154 int length = hexString.length(); 155 byte[] buffer = new byte[length / 2]; 156 157 for (int i = 0 ; i < length ; i += 2) 158 { 159 buffer[i / 2] = (byte)((toByte(hexString.charAt(i)) << 4) | toByte(hexString.charAt(i+1))); 160 } 161 162 return buffer; 163 } 164 } 165