Java中的INET_NTOA和INET_ATON? [英] INET_NTOA and INET_ATON in Java?

查看:527
本文介绍了Java中的INET_NTOA和INET_ATON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Java(使用Spring框架)并希望在IPv4地址的数字表示(例如2130706433)和它们的文本对应物(例如 127.0.0.1 )之间进行转换。通常,这样做的方法是用编程语言提供的(它们通常分别称为 INET_NTOA INET_ATON )但是我在Java中找不到它。

I use Java (with Spring framework) and want to convert between numeric representations of IPv4 addresses (e.g. 2130706433) and their textual counterparts (e.g. 127.0.0.1). Often, methods for doing this are supplied in programming languages (they're usually called INET_NTOA and INET_ATON respectively) but I can't find it in Java.

有人知道他们叫什么或者如何实现它们吗?

Anybody knows what they're called or how to implement them?

推荐答案

查看javadocs中的 InetAddress 。标准API不直接支持这些函数,但您可以使用此类提取这两个表示。
一个小例子:

Look at InetAddress in the javadocs. These functions are not directly supported by the Standard API but you can extract both representations using this class. A small example:

    InetAddress address = InetAddress.getLocalHost();
    byte[] byteAddress = address.getAddress();
    System.out.println(Arrays.toString(byteAddress));
    System.out.println(address.getHostAddress());

(请记住字节已签名。)

(Keep in mind that bytes are signed.)

如果你有长期而不是你可以使用 ByteBuffer ,快速舒适的转换。方法:putLong()然后是array()。

If you have long-s than yo can use ByteBuffer, for fast and comfortable coversion. Methods: putLong() then array().

这篇关于Java中的INET_NTOA和INET_ATON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆