1

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));
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Zbarcea Christian
  • 9,367
  • 22
  • 84
  • 137
  • A Java byte can store -128 to 127. If we interpret them unsigned, we can store 0 to 255. There is no way to store 8099 in a single byte. – jlordo Apr 27 '13 at 14:23
  • In your original string is `77` decimal or hexadecimal value? – PM 77-1 Apr 27 '13 at 14:26
  • When you say that your code "*is not working*", what does it actually mean? Are you getting any errors? Are you not getting the expected result? Please add all the details into your post. – PM 77-1 Apr 27 '13 at 14:35

6 Answers6

4

There is a special package in Java ti deal with internet addresses java.net, use it.

    String s = "77.125.65.201:8099";
    String[] a = s.split(":");
    InetAddress ia = InetAddress.getByName(a[0]);
    byte[] bytes = ia.getAddress();
    int port = Integer.parseInt(a[1]);
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
2
private String parseAddressToHex(String address) {

    int result = 0;
    String[] str = address.split("\\.");
    for (int i = 0; i < str.length; i++) {
        int j = Integer.parseInt(str[i]);
        result = result << 8 | (j & 0xFF);
    }
    return Integer.toHexString(result);
}
Jeffery Ma
  • 3,051
  • 1
  • 23
  • 26
1

replace your function toHex with this one.

private String toHex(String data){
   return Integer.toHexString(Integer.parseInt(data));
}
TheEwook
  • 11,037
  • 6
  • 36
  • 55
1

The thing is that is causing you problems here is that convert to bytes, you actually DON'T need to use "hex" at all. All you really need to do is convert 1-3 digit decimal numbers to bytes, and a 1-5 digit decimal number to a pair of bytes: e.g.

private byte[] parseIp(String fullData){
   String[] data = fullData.split(":"); 
   String[] ip = data[0].split("\\."); 
   byte[] res = new byte[6];

   for(int i = 0; i < 4; i++){
      res[i] = (byte) Integer.parseInt(ip[i]);
   }
   port = Integer.parseInt(data[1]);
   res[4] = (byte)((port >> 8) & 0xff);
   res[5] = (byte)(port & 0xff);
   return res;
}

(The above needs some error checking ...)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

You can use this code that i copied from: Convert a string representation of a hex dump to a byte array using Java?

public static byte[] hexStringToByteArray(String s) {
    int len = s.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                             + Character.digit(s.charAt(i+1), 16));
    }
    return data;
}
Community
  • 1
  • 1
Lukas Eichler
  • 5,689
  • 1
  • 24
  • 43
-1

your code works like charm for me, after making the compiler happy (java se7u21-x64 on win7). here comes the standalone .java file:

//
//  27.04.2013 16:26:32
//

public class  ipconvert {
    private static void parseIp(String fullData){
       String[] data = fullData.split(":"); // 8099
       String[] ip = data[0].split("\\."); // 77 | 125 | 65 | 201

       System.out.println();
       System.out.print("---> " + toHex(ip[0]));
       for(int i = 1; i < 4; i++){
          System.out.print("."+toHex(ip[i]));
       }
       System.out.println(":"+data[1]);
    }

    private static String toHex(String data){
       return Integer.toHexString(Integer.parseInt(data, 16));
    }

    public static void main(String[] args) {
        String stest;

        System.out.println("SO tests");
        System.out.println();
        stest = new String("77.125.65.201:8099");
        parseIp ( stest );
        System.out.println();
        return;
    }
}
collapsar
  • 17,010
  • 4
  • 35
  • 61