asked in 電腦與網際網路程式設計 · 10 years ago

幫我轉成 C CODE (JAVA寫的) 20點

import java.net.*;//导入包public class ip2long {

public static int str2Ip(String ip) throws UnknownHostException {

InetAddress address = InetAddress.getByName(ip);//在给定主机名的情况下确定主机的 IP 址。

byte[] bytes = address.getAddress();//返回此 InetAddress 对象的原始 IP 地址

int a, b, c, d;

a = byte2int(bytes[0]);

b = byte2int(bytes[1]);

c = byte2int(bytes[2]);

d = byte2int(bytes[3]);

int result = (a << 24) | (b << 16) | (c << 8) | d;

return result;

}public static int byte2int(byte b) {

int l = b & 0x07f;

if (b < 0) {

l |= 0x80;

}

return l;

}public static long ip2long(String ip) throws UnknownHostException {

int ipNum = str2Ip(ip);

return int2long(ipNum);

}public static long int2long(int i) {

long l = i & 0x7fffffffL;

if (i < 0) {

l |= 0x080000000L;

}

return l;

}public static String long2ip(long ip) {

int[] b = new int[4];

b[0] = (int) ( (ip >> 24) & 0xff);

b[1] = (int) ( (ip >> 16) & 0xff);

b[2] = (int) ( (ip >> 8) & 0xff);

b[3] = (int) (ip & 0xff);

String x;

Integer p;

p = new Integer(0);

x = p.toString(b[0]) + "." + p.toString(b[1]) + "." + p.toString(b[2]) +

"." + p.toString(b[3]);return x;}

//测试函数

public static void main(String[] args) throws Exception {

long ip = ip2long("1.0.0.0");

System.out.println(ip);

System.out.println(long2ip(ip));}}

1 Answer

Rating
  • 其威
    Lv 7
    10 years ago
    Favorite Answer

    這堆只是 format ip 的程式碼, 直接重寫應該會比較簡單.

    2011-04-26 21:36:08 補充:

    重寫比較快...

    C 沒有 exception, 所以我弄了個 error_code 用來放錯誤代碼.

    #include <stdio.h>

    #include <stdint.h>

    int error_code;

    #define E_OK 0 // no problem

    #define E_INVALIP 1 // invalid ip address

    #define E_FMT 2 // format error

    uint32_t str2ip(char *ip)

    {

    uint32_t a, b, c, d;

    if (sscanf(ip, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)

    {

    error_code = E_FMT;

    return 0;

    }

    if (a > 255 || b > 255 || c > 255 || d > 255)

    {

    error_code = E_INVALIP;

    return 0;

    }

    error_code = E_OK;

    return (a << 24) | (b << 16) | (c << 8) | d;

    }

    char * ip2str(char *dst, uint32_t ip)

    {

    sprintf(dst, "%u.%u.%u.%u",

    (ip & 0xff000000) >> 24,

    (ip & 0x00ff0000) >> 16,

    (ip & 0x0000ff00) >> 8,

    (ip & 0x000000ff)

    );

    return dst;

    }

    int main()

    {

    uint32_t ip;

    ip = str2ip("1.0.0.256"); // invalid ip

    if (error_code)

    printf("error: %d\n", error_code);

    else

    printf("%u\n", ip);

    ip = str2ip("1.2.3"); // wrong format

    if (error_code)

    printf("error: %d\n", error_code);

    else

    printf("%u\n", ip);

    ip = str2ip("192.168.1.254"); // ok

    if (error_code)

    printf("error: %d\n", error_code);

    else

    printf("%u\n", ip);

    char buf[16] = { 0 }; // longest "255.255.255.255" -> 15 chars + '\0' => 16

    printf("%s\n", ip2str(buf, ip));

    }

    2011-04-26 21:37:06 補充:

    要注意的是 error code 並不是 thread-safe.

    你要自己用 mutex 或 semaphore 鎖定 str2ip().

    2011-04-26 21:38:52 補充:

    並且, 因為 C 沒有 gc, 不太建議你在函式裡面 malloc() 暫存區 - 通常會忘記 free().

    所以要自己傳 buffer 進去.

Still have questions? Get your answers by asking now.