I need a method to convert a string "IP:PORT" into a byte array. I know how to format manually, but I need a way to do it automatically.
Example IP:
77.125.65.201:8099
I just can't use "".getBytes(); because I need the following format (without dot and colon):
[#1 octet ip] [#2 octet ip] [#3 octet ip] [#4 octet ip] [#1 2 octet port]
For a better understanding:
77 125 65 201 8099
In Java manually I can set it:
byte[] testIP = { 0x4D, 0x7D, 0x41, (byte)0xC9, (byte)0x1FA3 };
I need to find a method that will return a byte array in the correct format, casting to byte when it's necessary (because of Java signed bytes).
This is what have I made but it's not working:
private void parseIp(String fullData){
String[] data = fullData.split(":"); // 8099
String[] ip = data[0].split("\\."); // 77 | 125 | 65 | 201
for(int i = 0; i < 4; i++){
System.out.println("---> " + toHex(ip[i]));
}
}
private String toHex(String data){
return Integer.toHexString(Integer.parseInt(data, 16));
}