Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , , ,

Sponsored Links



Convert IP address to array of bytes

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 03-03-2010
Member
 
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.
Reply With Quote
  #2  
Old 03-03-2010
Member
 
Join Date: Nov 2009
Posts: 520
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.
Reply With Quote
  #3  
Old 03-03-2010
Member
 
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]);
}
Reply With Quote
  #4  
Old 03-03-2010
Member
 
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.
Reply With Quote
  #5  
Old 03-03-2010
Member
 
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.
Reply With Quote
  #6  
Old 03-03-2010
Member
 
Join Date: Nov 2009
Posts: 447
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] );
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Convert IP address to array of bytes"
Thread Thread Starter Forum Replies Last Post
How to convert array to collection in java? Steadfast Software Development 4 06-02-2010 04:22 PM
Store content of file in array of bytes Aaliya Seth Software Development 5 21-01-2010 01:25 PM
How to convert bytes to bits CitricAcid Software Development 3 14-08-2009 06:51 PM
How to convert Bytes to Megabytes Girish-S India BroadBand 2 15-06-2009 03:47 PM
Convert a string to bytes $tatic Software Development 4 18-02-2009 02:29 PM


All times are GMT +5.5. The time now is 07:41 AM.