Autopsy  4.20.0
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 
44  @Deprecated
45  public static String byteArrayToHex(byte[] array, int length, long arrayOffset, Font font) {
46  return byteArrayToHex(array, length, arrayOffset);
47  }
48 
59  public static String byteArrayToHex(byte[] array, int length, long arrayOffset) {
60  if (array == null) {
61  return "";
62  } else {
63  StringBuilder outputStringBuilder = new StringBuilder();
64 
65  // loop through the file in 16-byte increments
66  for (int curOffset = 0; curOffset < length; curOffset += 16) {
67  // how many bytes are we displaying on this line
68  int lineLen = 16;
69  if (length - curOffset < 16) {
70  lineLen = length - curOffset;
71  }
72 
73  // print the offset column
74  //outputStringBuilder.append("0x");
75  outputStringBuilder.append(String.format("0x%08x: ", arrayOffset + curOffset)); //NON-NLS
76  //outputStringBuilder.append(": ");
77 
78  // print the hex columns
79  for (int i = 0; i < 16; i++) {
80  if (i < lineLen) {
81  int v = array[curOffset + i] & 0xFF;
82  outputStringBuilder.append(hexArray[v >>> 4]);
83  outputStringBuilder.append(hexArray[v & 0x0F]);
84  } else {
85  outputStringBuilder.append(" ");
86  }
87 
88  // someday we'll offer the option of these two styles...
89  if (true) {
90  outputStringBuilder.append(" ");
91  if (i % 4 == 3) {
92  outputStringBuilder.append(" ");
93  }
94  if (i == 7) {
95  outputStringBuilder.append(" ");
96  }
97  } // xxd style
98  else {
99  if (i % 2 == 1) {
100  outputStringBuilder.append(" ");
101  }
102  }
103  }
104 
105  outputStringBuilder.append(" ");
106 
107  // print the ascii columns
108  String ascii = new String(array, curOffset, lineLen, java.nio.charset.StandardCharsets.US_ASCII);
109  for (int i = 0; i < 16; i++) {
110  char c = ' ';
111  if (i < ascii.length()) {
112  c = ascii.charAt(i);
113  int dec = (int) c;
114 
115  if (dec < 32 || dec > 126) {
116  c = '.';
117  }
118  }
119  outputStringBuilder.append(c);
120  }
121 
122  outputStringBuilder.append("\n");
123  }
124 
125  return outputStringBuilder.toString();
126  }
127  }
128 
129  protected static String charArrayToByte(char[] array) {
130  if (array == null) {
131  return "";
132  } else {
133  String[] binary = new String[array.length];
134 
135  for (int i = 0; i < array.length; i++) {
136  binary[i] = Integer.toBinaryString(array[i]);
137  }
138  return Arrays.toString(binary);
139  }
140  }
141 }
static String byteArrayToHex(byte[] array, int length, long arrayOffset)
static String byteArrayToHex(byte[] array, int length, long arrayOffset, Font font)

Copyright © 2012-2022 Basis Technology. Generated on: Tue Aug 1 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.