Autopsy  3.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
DataConversion.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.datamodel;
20 
21 import java.awt.Font;
22 import java.util.Arrays;
23 import java.util.Formatter;
24 
28 public class DataConversion {
29 
30  final private static char[] hexArray = "0123456789ABCDEF".toCharArray(); //NON-NLS
31 
41  @Deprecated
42  public static String byteArrayToHex(byte[] array, int length, long arrayOffset, Font font) {
43  return byteArrayToHex(array, length, arrayOffset);
44  }
45 
53  public static String byteArrayToHex(byte[] array, int length, long arrayOffset) {
54  if (array == null) {
55  return "";
56  }
57  else {
58  StringBuilder outputStringBuilder = new StringBuilder();
59 
60  // loop through the file in 16-byte increments
61  for (int curOffset = 0; curOffset < length; curOffset += 16) {
62  // how many bytes are we displaying on this line
63  int lineLen = 16;
64  if (length - curOffset < 16) {
65  lineLen = length - curOffset;
66  }
67 
68  // print the offset column
69  //outputStringBuilder.append("0x");
70  outputStringBuilder.append(String.format("0x%08x: ", arrayOffset + curOffset)); //NON-NLS
71  //outputStringBuilder.append(": ");
72 
73  // print the hex columns
74  for (int i = 0; i < 16; i++) {
75  if (i < lineLen) {
76  int v = array[curOffset + i] & 0xFF;
77  outputStringBuilder.append(hexArray[v >>> 4]);
78  outputStringBuilder.append(hexArray[v & 0x0F]);
79  }
80  else {
81  outputStringBuilder.append(" ");
82  }
83 
84  // someday we'll offer the option of these two styles...
85  if (true) {
86  outputStringBuilder.append(" ");
87  if (i % 4 == 3) {
88  outputStringBuilder.append(" ");
89  }
90  if (i == 7) {
91  outputStringBuilder.append(" ");
92  }
93  }
94  // xxd style
95  else {
96  if (i % 2 == 1) {
97  outputStringBuilder.append(" ");
98  }
99  }
100  }
101 
102  outputStringBuilder.append(" ");
103 
104  // print the ascii columns
105  String ascii = new String(array, curOffset, lineLen, java.nio.charset.StandardCharsets.US_ASCII);
106  for (int i = 0; i < 16; i++) {
107  char c = ' ';
108  if (i < ascii.length()) {
109  c = ascii.charAt(i);
110  int dec = (int) c;
111 
112  if (dec < 32 || dec > 126) {
113  c = '.';
114  }
115  }
116  outputStringBuilder.append(c);
117  }
118 
119  outputStringBuilder.append("\n");
120  }
121 
122  return outputStringBuilder.toString();
123  }
124  }
125 
126  protected static String charArrayToByte(char[] array) {
127  if (array == null) {
128  return "";
129  } else {
130  String[] binary = new String[array.length];
131 
132  for (int i = 0; i < array.length; i++) {
133  binary[i] = Integer.toBinaryString(array[i]);
134  }
135  return Arrays.toString(binary);
136  }
137  }
138 }
static String byteArrayToHex(byte[] array, int length, long arrayOffset)
static String byteArrayToHex(byte[] array, int length, long arrayOffset, Font font)

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.