-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHex.java
More file actions
37 lines (33 loc) · 1007 Bytes
/
Copy pathHex.java
File metadata and controls
37 lines (33 loc) · 1007 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
// Success in 0.34s
import java.util.Scanner;
public class Hex{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
char[] line = sc.nextLine().toCharArray();
boolean good = false;
String hex = "";
for(int i = 0; i < line.length; i++){
if(good){
if(goodChar(line[i]) && hex.length() < 8){
hex += line[i];
}else{
good = false;
if(hex.equals("")) continue;
System.out.printf("0x%s %d\n", hex, Long.parseLong(hex, 16));
hex = "";
}
}else if(Character.toLowerCase(line[i]) == 'x' && i != 0 && line[i - 1] == '0'){
good = true;
}
}
if(!hex.equals("")){
System.out.printf("0x%s %d\n", hex, Long.parseLong(hex, 16));
}
}
sc.close();
}
private static boolean goodChar(char c){
return c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F';
}
}