Ho cercato molto ma non sono riuscito a trovare alcun modo per convertire il tipo inetaddress in stringa (potrebbe essere la mia ricerca non è buona: S). Devo convertirlo in tipo stringa perché io devi mostrare il risultato in textarea (componente gui) che richiede un tipo di stringa. Qualcuno può come è possibile?Java: inetaddress to String conversion
risposta
API Java per la classe InetAddress ha buoni metodi per le vostre esigenze, vedo. Prova questi metodi. Puoi consultare altri getter di proprietà di InetAddress per i tuoi ulteriori requisiti.
public static void main (String [] args) throws UnknownHostException
{
// Instantiate your own InetAddress object.
InetAddress address = InetAddress.getLocalHost();
String hostIP = address.getHostAddress() ;
String hostName = address.getHostName();
System.out.println("IP: " + hostIP + "\n" + "Name: " + hostName);
}
Hai provato a utilizzare il metodo InetAddress.toString()?
Che dire
System.out.println(Inet4Address.getLocalHost().getHostAddress());
?
Basta chiamare il metodo InetAddress toString(). Inoltre, se si desidera specificamente il nome host, utilizzare getHostName. Se si desidera una rappresentazione in formato stringa dell'indirizzo IP, utilizzare getHostAddress().
campione:
InetAddress inet = InetAddress.getByName("193.125.22.1");
System.out.println(inet.toString());
Per maggiori informazioni consultare: http://www.java2s.com/Code/JavaAPI/javax.net/InetAddressgetByNameStringname.htm
upvoted per il bit importante: getHostAddress se si desidera solo la stringa di indirizzo –
- 1. Ruby String to Date Conversion
- 2. svit to git conversion
- 3. kCVPixelFormatType_420YpCbCr8BiPlanarFullRange frame to UIImage conversion
- 4. String to HashMap JAVA
- 5. JAVA String to char
- 6. JAVA Specifica porta con InetAddress
- 7. Map to String in Java
- 8. Ruby url to html link conversion
- 9. DateTimeOffset to DateTime conversion - Data Loss
- 10. Java Generics type conversion puzzle
- 11. Come convertire String to Reader in java
- 12. InetSocketAddress vs InetAddress
- 13. InetAddress in Android
- 14. Esiste un modo semplice per convertire String in Inetaddress in Java?
- 15. Java: JSON -> Protobuf & back conversion
- 16. Tuple to string
- 17. object to string array
- 18. Lua string to int
- 19. String Fraction to Double
- 20. Coercing String to & str
- 21. JSONResult to String
- 22. Specificatore Float to String
- 23. Python Decimal to String
- 24. JSON string to hack
- 25. String to LocalDate
- 26. C# GZipStream to String
- 27. Haxe Int to String
- 28. Injecting string to 'cin'
- 29. Binary String to Integer
- 30. Parse String to Double.NaN
Questo restituisce una barra. Utilizzare invece getHostAddress(). Vedi http://stackoverflow.com/questions/12947435/inetaddress-tostring-returns-a-forward-slash – LeslieM