ip地址转成8位十六进制串


/** ip转16进制 */
public static String ipToHex(String ips) {
    StringBuffer result = new StringBuffer();
    if (ips != null) {
        StringTokenizer st = new StringTokenizer(ips, ".");
        while (st.hasMoreTokens()) {
            String token = Integer.toHexString(Integer.parseInt(st.nextToken()));
            if (token.length() == 1)
                token = "0" + token;
            result.append(token);
        }
    }
    return result.toString();
}

/** 16进制转ip */
public static String texToIp(String ips) {
    try {
        StringBuffer result = new StringBuffer();
        if (ips != null && ips.length() == 8) {
            for (int i = 0; i < 8; i += 2) {
                if (i != 0)
                    result.append('.');
                result.append(Integer.parseInt(ips.substring(i, i + 2), 16));
            }
        }
        return result.toString();
    } catch (NumberFormatException ex) {
        Logger.e(ex);
    }
    return "";
}
相关标签

扫一扫

在手机上阅读