Results 1 to 6 of 6

Thread: Convert IP address to array of bytes

  1. #1
    Join Date
    Dec 2009
    Posts
    296

    Convert IP address to array of bytes

    Hello,
    My problem: I have a String that contains an IP address and I would convert every byte in an array of byte []. This is what I try to do for achieving this, but unfortunately this is not working
    Here is my code
    Code:
    StringTokenizer stok = new StringTokenizer(sourceIPAddress, ".");
    String oc4 =  "0x" + Integer.toHexString(Integer.parseInt(stok.nextToken()));
    String oc3 =  "0x" + Integer.toHexString(Integer.parseInt(stok.nextToken()));
    String by2 =  "0x" + Integer.toHexString(Integer.parseInt(stok.nextToken()));
    String by1 =  "0x" + Integer.toHexString(Integer.parseInt(stok.nextToken()));
    Any help on this is highly appreciated. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    518

    Re: Convert IP address to array of bytes

    Hello,
    You can pass instead of the integer to a Byte.
    Here is the method, how you can do this
    Code:
    Byte mnby = (byte) Integer.parseInt(token)
    where token is a part of your address. And attention to the limits, because it may give any exception for the code if not used properly. If you have any more queries regarding this topic then you can post and we will try to solve your query.

  3. #3
    Join Date
    Nov 2009
    Posts
    583

    Re: Convert IP address to array of bytes

    Hello,
    Here is an sample example for the same, see, if this helps you.
    Code:
    String st = "IP address";
    String[] strar = st.split("\\.");
    Short[] sh = new Short[Aft.length];
    for(int i =0; i <strar.length; i + +){
      sh[i]=Short.function valueOf() {
        [native code]
    }(strar[i]);
    }

  4. #4
    Join Date
    Nov 2009
    Posts
    359

    Re: Convert IP address to array of bytes

    Hello,
    The Class InetAddress offers everything you need:
    Here is an example of what it is capable of.
    Code:
    	InetAddress ad = InetAddress.getByName("IP address ");
    	byte[] ip = ad.getAddress();
    The java.io and java.net packages are important in your case. I recommend you to study the java.io and the java.net packages and then code for your program what you are trying to develop.

  5. #5
    Join Date
    Dec 2009
    Posts
    296

    Re: Convert IP address to array of bytes

    Hello,
    I have tried this code, the InetAddress class does not do what I want.
    Here is the code which I have tried
    Code:
    InetAddress ad = InetAddress.getByName("IP address");
    byte[] by = ad.getAddress();
    System.out.System.out.println(by[0]);
    System.out.System.out.println(by[1]);
    System.out.System.out.println(by[2]);
    System.out.System.out.println(by[3]);
    If you have any other alternative method which can do the same or give the same result then please do post back.

  6. #6
    Join Date
    Nov 2009
    Posts
    446

    Re: Convert IP address to array of bytes

    Hello,
    Just have a look at the following part of code
    Code:
    int oc = Integer.parseInt(st.nextToken());
    Indeed it is no use to format a number to string for call in number. In short your problem is a display problem.
    Code:
    	InetAddress ad = InetAddress.getByName("IP address");
    	byte[] by = ad.getAddress();
    And this
    Code:
    System.out.printf("% # x% # x% # x% # x% n", i[0], i[1], i[2], i[3] );

Similar Threads

  1. SanDisk flash drive “0 bytes used and 0 bytes free”
    By Coloma in forum Hardware Peripherals
    Replies: 5
    Last Post: 03-12-2012, 10:56 AM
  2. Store content of file in array of bytes
    By Aaliya Seth in forum Software Development
    Replies: 5
    Last Post: 21-01-2010, 01:25 PM
  3. How to convert bytes to bits
    By CitricAcid in forum Software Development
    Replies: 3
    Last Post: 14-08-2009, 05:51 PM
  4. How to convert Bytes to Megabytes
    By Girish-S in forum India BroadBand
    Replies: 2
    Last Post: 15-06-2009, 02:47 PM
  5. Convert a string to bytes
    By $tatic in forum Software Development
    Replies: 4
    Last Post: 18-02-2009, 02:29 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,203,676.04959 seconds with 16 queries