-1

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.

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

I know I'm missing something simple but I'm drawing a blank.

n0vo
  • 11
  • 2
  • There's a lot of undefined behavior here. You're basically just copying the first 8 characters into a long slot and printing a non-null-terminated char pointer. There is no conversion into a long happening really, and it's fortunate that the print-out doesn't keep going. I suggest that you read a primer on C--perhaps the K&R book. – synchronizer Jan 30 '17 at 21:59
  • 1
    Everything is wrong there, you better go back to your C book and come back when you understand a little more of how all of that works. – fernando.reyes Jan 30 '17 at 21:59
  • [Here's a starting point](https://stackoverflow.com/questions/16327077). – user3386109 Jan 30 '17 at 22:04
  • 1
    The thing you are missing is that this isn't supposed to work. – user253751 Jan 30 '17 at 22:07
  • I don't see any code to convert anything. I see code that tries to copy a string into something that's supposed to hold a long and then something that tries to print a string from something that holds a long. Where is the code to actually *convert* anything? – David Schwartz Jan 30 '17 at 23:52

2 Answers2

3

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; neither does a cast from long to char * do the opposite in your printf-statement.

Converting char* to long is done using atol, whereas printing a long to console is done through printf("%ld",l)

Good luck and patience on your way to get an experienced C programmer, and don't hesitate to ask :-)

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
2

Here is a program that takes an IP address in the form of an input string, and converts it to a long value. The filter_ip() function is used to first remove all non-numeric characters from the string. Then strtol() is used to convert the string to a long value.

It is better to use strtol() than atol() because strtol() detects integer overflows. errno is declared in the error.h header file, and strtol() sets the value of errno if there is an overflow. strtol() converts as many characters as it can, starting from the beginning of the string, and sets tailptr to point the the remaining characters. If no conversion can be performed, tailptr will point to the beginning of the string.

#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
ad absurdum
  • 19,498
  • 5
  • 37
  • 60