简体   繁体   English

将IP地址char转换为long

[英]Converting IP address char to long

I wrote a small program to convert a char to a long, however when I try to print the char form, it has mangled output. 我编写了一个小程序将char转换为long,但是当我尝试打印char表单时,它的输出混乱了。

int main()
{
    long l;
    char *buf = "127.0.0.1";
    memcpy(&l, buf, sizeof(long));
    printf("%s\n", (char *)&l);

    return 0;
}

The output looks like this: 输出看起来像这样:

127.0.0.t 턾U 127.0.0.t 턾U

I know I'm missing something simple but I'm drawing a blank. 我知道我缺少一些简单的东西,但我正在画空白。

Seems as if there are some basic misunderstandings :-) 似乎有一些基本的误解:-)

Copying between char * and long data type does not convert a "number" represented by a sequence of digits into a "binary" number like long; char *long数据类型之间进行复制不会将由数字序列表示的“数字”转换为类似于long的“二进制”数字。 neither does a cast from long to char * do the opposite in your printf -statement. longchar *也不会在printf语句中执行相反的操作。

Converting char* to long is done using atol , whereas printing a long to console is done through printf("%ld",l) 使用atol可以将char*转换为long,而通过printf("%ld",l)将long打印到控制台

Good luck and patience on your way to get an experienced C programmer, and don't hesitate to ask :-) 祝您有经验的C程序员,祝您好运和耐心,不要犹豫了:-)

Here is a program that takes an IP address in the form of an input string, and converts it to a long value. 这是一个程序,它以输入字符串的形式获取IP地址,并将其转换为long值。 The filter_ip() function is used to first remove all non-numeric characters from the string. filter_ip()函数用于首先从字符串中删除所有非数字字符。 Then strtol() is used to convert the string to a long value. 然后使用strtol()将字符串转换为long值。

It is better to use strtol() than atol() because strtol() detects integer overflows. 最好使用strtol()不是atol()因为strtol()会检测整数溢出。 errno is declared in the error.h header file, and strtol() sets the value of errno if there is an overflow. errnoerror.h头文件中声明,并且strtol()设置errno的值(如果发生溢出)。 strtol() converts as many characters as it can, starting from the beginning of the string, and sets tailptr to point the the remaining characters. 从字符串的开头开始, strtol()转换尽可能多的字符,并设置tailptr指向其余字符。 If no conversion can be performed, tailptr will point to the beginning of the string. 如果无法执行任何转换,则tailptr将指向字符串的开头。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

void filter_ip(char *str);

int main(void)
{
    char ip_addr[] = "127.0.0.1";
    long res;
    char *tailptr;

    printf("IP Address: %s\n", ip_addr);
    filter_ip(ip_addr);

    errno = 0;
    res = strtol(ip_addr, &tailptr, 10);
    if (errno) {
        perror("Unable to convert IP address");
    } else if (tailptr == ip_addr) {
        fprintf(stderr, "No conversion performed\n");
    } else {
        printf("%ld\n", res);
    }

    return 0;
}

void filter_ip(char *str)
{
    size_t i, j;

    for (i = 0, j = 0; str[i] != '\0'; i++) {
        if (isdigit(str[i])) {
            str[j++] = str[i];
        }
    }
    str[j] = '\0';
}

Program output: 程序输出:

IP Address: 127.0.0.1
127001

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM