forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHexadecimalOutput.java
More file actions
38 lines (24 loc) · 838 Bytes
/
HexadecimalOutput.java
File metadata and controls
38 lines (24 loc) · 838 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.zetcode;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
// the example reads a binary image into an array of bytes
// and write the bytes in hexadecimal format to the terminal
public class HexadecimalOutput {
public static void main(String[] args) throws IOException {
var fileName = "src/resources/ball.png";
try (InputStream is = new FileInputStream(fileName)) {
byte[] buffer = new byte[is.available()];
is.read(buffer);
int i = 0;
for (byte b: buffer) {
if (i % 10 == 0) {
System.out.println();
}
System.out.printf("%02x ", b);
i++;
}
}
System.out.println();
}
}