|
| |||||||||
| Tags: array, bytes, converting, datatypes, internet protocol, ip address, string |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| 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())); |
|
#2
| |||
| |||
| 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) |
|
#3
| |||
| |||
| 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
| |||
| |||
| 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(); |
|
#5
| |||
| |||
| 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]); |
|
#6
| |||
| |||
| 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()); Code: InetAddress ad = InetAddress.getByName("IP address");
byte[] by = ad.getAddress(); Code: System.out.printf("% # x% # x% # x% # x% n", i[0], i[1], i[2], i[3] ); |
![]() |
|
| Thread Tools | Search this Thread |
| |
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 |