Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Test {
- public static void main(String [] args) {
- String datagram = "45 00 00 4e f7 fa 40 00 38 06 9d 33 d4 b6 18 1b c0 a8 00 02 0b 54 b9 a6 fb f9 3c 57 c1 0a 06 c1 80 18 00 e3 ce 9c 00 00 01 01 08 0a 03 a6 eb 01 00 0b f8 e5 6e 65 74 77 6f 72 6b 20 70 72 6f 67 72 61 6d 6d 69 6e 67 20 69 73 20 66 75 6e";
- String datagram2 = datagram.replace(" ", "");
- String version = datagram2.substring(0,1);
- String protokol = datagram2.substring(19,20);
- String sourceAddress1 = datagram2.substring(24,26);
- String sourceAddress2 = datagram2.substring(26,28);
- String sourceAddress3 = datagram2.substring(28,30);
- String sourceAddress4 = datagram2.substring(30,32);
- String destinationAddress1 = datagram2.substring(32,34);
- String destinationAddress2 = datagram2.substring(34,36);
- String destinationAddress3 = datagram2.substring(36,38);
- String destinationAddress4 = datagram2.substring(37,40);
- int numberVersion= Integer.parseInt(version.trim(), 16);
- int numberProtokol = Integer.parseInt(protokol.trim(), 16);
- int numberSourceAddress1 = Integer.parseInt(sourceAddress1.trim(), 16);
- int numberSourceAddress2 = Integer.parseInt(sourceAddress2.trim(), 16);
- int numberSourceAddress3 = Integer.parseInt(sourceAddress3.trim(), 16);
- int numberSourceAddress4 = Integer.parseInt(sourceAddress4.trim(), 16);
- int numberDestinationAddress1 = Integer.parseInt(destinationAddress1.trim(), 16);
- int numberDestinationAddress2 = Integer.parseInt(destinationAddress2.trim(), 16);
- int numberDestinationAddress3 = Integer.parseInt(destinationAddress3.trim(), 16);
- int numberDestinationAddress4 = Integer.parseInt(destinationAddress4.trim(), 16);
- System.out.println("wersja: " + numberVersion);
- System.out.println("protokol:" + numberProtokol);
- System.out.println("adres źródłowy: " + numberSourceAddress1 + "." + numberSourceAddress2 + "."
- + numberSourceAddress3 + "." + numberSourceAddress4);
- System.out.println("adres docelowy: " + numberDestinationAddress1 + "." + numberDestinationAddress2 + "."
- + numberDestinationAddress3 + "." + numberDestinationAddress4);
- if(numberProtokol == 6){
- String sourcePort = datagram2.substring(40, 44).replace(" ", "");
- String destinationPort = datagram2.substring(44, 48).replace(" ", "");
- int numberSourcePort = Integer.parseInt(sourcePort.trim(), 16);
- int numberDestinationPort = Integer.parseInt(destinationPort.trim(), 16);
- System.out.println("Numer portu zrodlowego: " + numberSourcePort);
- System.out.println("Numer portu docelowego: " + numberDestinationPort);
- String data = datagram2.substring(104).replace(" ", "");
- StringBuilder output = new StringBuilder();
- for (int i = 0; i < data.length(); i+=2) {
- String str = data.substring(i, i+2);
- output.append((char)Integer.parseInt(str.trim(), 16));
- }
- System.out.println(output);
- }
- else if(numberProtokol == 17){
- String sourcePort = datagram2.substring(40, 44).replace(" ", "");
- String destinationPort = datagram2.substring(44, 48).replace(" ", "");
- int numberSourcePort = Integer.parseInt(sourcePort.trim(), 16);
- int numberDestinationPort = Integer.parseInt(destinationPort.trim(), 16);
- System.out.println("Numer portu zrodlowego: " + numberSourcePort);
- System.out.println("Numer portu docelowego: " + numberDestinationPort);
- String data = datagram.substring(56).replace(" ", "");
- StringBuilder output = new StringBuilder();
- for (int i = 0; i < data.length(); i += 2) {
- String str = data.substring(i, i + 2);
- output.append((char) Integer.parseInt(str, 16));
- }
- System.out.println(output);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement