Hex dump: Difference between revisions

Content deleted Content added
WaGi (talk | contribs)
Added PHP
PSNOW123 (talk | contribs)
New post.
Line 485: Line 485:
00000066 00101110 00000000 |..|
00000066 00101110 00000000 |..|
0000006c
0000006c
</pre>

=={{header|Java}}==
<syntaxhighlight lang="java">
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public final class HexDump {

public static void main(String[] args) throws IOException {
byte[] bytes = Files.readAllBytes(Path.of("ExampleUTF16LE.dat"));
System.out.println("Hex dump of the entire file:");
System.out.println( new Converter(bytes, 0, bytes.length, DumpType.HEX).toConvertedString());
System.out.println();
System.out.println("xxd dump of the entire file:");
System.out.println( new Converter(bytes, 0, bytes.length, DumpType.XXD).toConvertedString());
System.out.println();
System.out.println("Hex dump of the file from byte 20 to byte 94:");
System.out.println( new Converter(bytes, 20, 75, DumpType.HEX).toConvertedString());
System.out.println();
System.out.println("xxd dump of the file from byte 38 to byte 58:");
System.out.println( new Converter(bytes, 38, 21, DumpType.HEX).toConvertedString());
}

private static final class Converter {
public Converter(byte[] aBytes, int aStartIndex, int aLength, DumpType aDumpType) {
startIndex = Math.min(aBytes.length, Math.max(0, aStartIndex));
length = ( aLength < 1 || aLength > aBytes.length - startIndex ) ? aBytes.length - startIndex : aLength;
dumpType = aDumpType;
bytes = IntStream.range(startIndex, startIndex + length).mapToObj( i -> aBytes[i] ).toList();
}
public String toConvertedString() {
return IntStream.rangeClosed(0, bytes.size() / dumpType.factor)
.mapToObj( i -> byteList(i * dumpType.factor, dumpType.factor) )
.map( list -> toConvertedRow(list) )
.collect(Collectors.joining())
+ toHex(counterFinish);
}
private List<Byte> byteList(int currentIndex, int currentLength) {
counterStart = currentIndex;
counterFinish = Math.min(currentIndex + currentLength, bytes.size());
return bytes.subList(currentIndex, Math.min(currentIndex + currentLength, bytes.size()));
}
private String toConvertedRow(List<Byte> row) {
String line = row.stream().map( b -> toDigit(b) ).collect(Collectors.joining());
line = padding(insertSpace(line));
return toHex(counterStart) + line + "|" +
row.stream().map( b -> printableChar(b) ).collect(Collectors.joining()) + "|" + "\n";
}
private String insertSpace(String line) {
return Pattern.compile(".{1," + dumpType.blockLength + "}")
.matcher(line).results().map(MatchResult::group).collect(Collectors.joining(" "));
}
private String toDigit(int number) {
return switch ( dumpType ) {
case HEX -> String.format("%02x ", number & 0xff);
case XXD -> Integer.toBinaryString( ( 1 << 8 | number & 0xff ) ).substring(1);
};
}
private String printableChar(int number) {
return ( number >= 32 && number < 127 ) ? String.valueOf((char) number) : ".";
}
private String padding(String line) {
return String.format("%1$-" + dumpType.lineLength + "s", line);
}
private String toHex(int number) {
return String.format("%08x ", number);
}
private int counterStart, counterFinish;
private final int startIndex, length;
private final List<Byte> bytes;
private final DumpType dumpType;
}
private enum DumpType {
HEX(16, 24, 50), XXD(6, 8, 55);
private DumpType(int aFactor, int aBlockLength, int aLineLength) {
factor = aFactor;
blockLength = aBlockLength;
lineLength = aLineLength;
}
private final int factor, blockLength, lineLength;
}

}
</syntaxhighlight>
{{ out }}
<pre>
Hex dump of the entire file:
00000000 ff fe 52 00 6f 00 73 00 65 00 74 00 74 00 61 00 |..R.o.s.e.t.t.a.|
00000010 20 00 43 00 6f 00 64 00 65 00 20 00 69 00 73 00 | .C.o.d.e. .i.s.|
00000020 20 00 61 00 20 00 70 00 72 00 6f 00 67 00 72 00 | .a. .p.r.o.g.r.|
00000030 61 00 6d 00 6d 00 69 00 6e 00 67 00 20 00 63 00 |a.m.m.i.n.g. .c.|
00000040 68 00 72 00 65 00 73 00 74 00 6f 00 6d 00 61 00 |h.r.e.s.t.o.m.a.|
00000050 74 00 68 00 79 00 20 00 73 00 69 00 74 00 65 00 |t.h.y. .s.i.t.e.|
00000060 20 00 3d d8 00 de 2e 00 | .=.....|
00000068

xxd dump of the entire file:
00000000 11111111 11111110 01010010 00000000 01101111 00000000 |..R.o.|
00000006 01110011 00000000 01100101 00000000 01110100 00000000 |s.e.t.|
0000000c 01110100 00000000 01100001 00000000 00100000 00000000 |t.a. .|
00000012 01000011 00000000 01101111 00000000 01100100 00000000 |C.o.d.|
00000018 01100101 00000000 00100000 00000000 01101001 00000000 |e. .i.|
0000001e 01110011 00000000 00100000 00000000 01100001 00000000 |s. .a.|
00000024 00100000 00000000 01110000 00000000 01110010 00000000 | .p.r.|
0000002a 01101111 00000000 01100111 00000000 01110010 00000000 |o.g.r.|
00000030 01100001 00000000 01101101 00000000 01101101 00000000 |a.m.m.|
00000036 01101001 00000000 01101110 00000000 01100111 00000000 |i.n.g.|
0000003c 00100000 00000000 01100011 00000000 01101000 00000000 | .c.h.|
00000042 01110010 00000000 01100101 00000000 01110011 00000000 |r.e.s.|
00000048 01110100 00000000 01101111 00000000 01101101 00000000 |t.o.m.|
0000004e 01100001 00000000 01110100 00000000 01101000 00000000 |a.t.h.|
00000054 01111001 00000000 00100000 00000000 01110011 00000000 |y. .s.|
0000005a 01101001 00000000 01110100 00000000 01100101 00000000 |i.t.e.|
00000060 00100000 00000000 00111101 11011000 00000000 11011110 | .=...|
00000066 00101110 00000000 |..|
00000068

Hex dump of the file from byte 20 to byte 94:
00000000 6f 00 64 00 65 00 20 00 69 00 73 00 20 00 61 00 |o.d.e. .i.s. .a.|
00000010 20 00 70 00 72 00 6f 00 67 00 72 00 61 00 6d 00 | .p.r.o.g.r.a.m.|
00000020 6d 00 69 00 6e 00 67 00 20 00 63 00 68 00 72 00 |m.i.n.g. .c.h.r.|
00000030 65 00 73 00 74 00 6f 00 6d 00 61 00 74 00 68 00 |e.s.t.o.m.a.t.h.|
00000040 79 00 20 00 73 00 69 00 74 00 65 |y. .s.i.t.e|
0000004b

xxd dump of the file from byte 38 to byte 58:
00000000 70 00 72 00 6f 00 67 00 72 00 61 00 6d 00 6d 00 |p.r.o.g.r.a.m.m.|
00000010 69 00 6e 00 67 |i.n.g|
00000015
</pre>
</pre>