Re: Can this conversion code be simplified?




Joe Wright wrote:
Eric Lilja wrote:
Hello, I have IPv4-numbers in the following format:
"\\x0A\\x11\\x8C\\x01"

Now I need each byte as an int. I wrote the following test program:
#include <stdio.h>
#include <stdlib.h>

static void to_ip(const char *, int *);
static void extract_substring(const char *, u_short, u_short, char *);

int
main(void)
{
const char *ip_as_string="\\x0A\\x11\\x8C\\x01";

int ip[4];

to_ip(ip_as_string, ip);

printf("IP is %i.%i.%i.%i\n", ip[0], ip[1], ip[2], ip[3]);

return 0;
}

static void
extract_substring(const char *s, u_short start, u_short end, char *out)
{
u_short out_index = 0;

for(; start <= end; ++start)
out[out_index++] = s[start];

out[++start] = '\0';
}

static void
to_ip(const char *ip_as_string, int *ip)
{
char sub[5] = "0";
char *ptr = sub;
u_short start_index = 1;
u_short end_index = 3;
u_short i = 0;

for(; i < 4; ++i)
{
extract_substring(ip_as_string, start_index, end_index, ptr + 1);

start_index += 4;
end_index += 4;

ip[i] = strtol(sub, NULL, 16);
}
}

It compiles cleanly and produces the following output:
$ ./convert_to_ip.exe
IP is 10.17.140.1


But since I am very poor on string manipulations in C, I'm guessing
there are alot cleaner ways to do this. Please help me improve the
code. It's a homework problem, but as you can see I attempted to solve
it myself first.

/ E

I don't know yet. When I compile the code you posted I get..

s2ip.c:5: parse error before "u_short"
s2ip.c:19: parse error before "u_short"
s2ip.c: In function `extract_substring':
s2ip.c:21: `u_short' undeclared (first use in this function)
s2ip.c:21: (Each undeclared identifier is reported only once
s2ip.c:21: for each function it appears in.)
s2ip.c:21: parse error before "out_index"
s2ip.c:22: `start' undeclared (first use in this function)
s2ip.c:22: `end' undeclared (first use in this function)
s2ip.c:23: `out' undeclared (first use in this function)
s2ip.c:23: `out_index' undeclared (first use in this function)
s2ip.c:23: `s' undeclared (first use in this function)
s2ip.c: In function `to_ip':
s2ip.c:32: `u_short' undeclared (first use in this function)
s2ip.c:32: parse error before "start_index"
s2ip.c:35: `i' undeclared (first use in this function)
s2ip.c:36: `start_index' undeclared (first use in this function)
s2ip.c:36: `end_index' undeclared (first use in this function)

Which puts me off a bit.

Hmm, I'm compiling using gcc with the flags -ansi -pedantic and I
thought that would turn off any compiler extension. Anyway, u_short is
simply a typedef for unsigned short int.


--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---

/ E

.



Relevant Pages

  • Problem in compiling a C code with MSVC++6.00
    ... When I save the file as a cpp file, it compiles and runs ... int p1; ... void checkfile; ... printf("cannot allocate memory for elements!"); ...
    (comp.lang.c)
  • Re: Member template internal compiler error
    ... I'm fairly sure it is well-formed (g++ 3.4.2 compiles it fine). ... template <int N> ... static void Test() ... function template. ...
    (microsoft.public.vc.language)
  • Re: Errors in compilation of simple programme
    ... using namespace std; ... cin>> i; ... it compiles without a problem on Visual C++ ... int main ...
    (microsoft.public.vc.language)
  • Re: Prime Numbers
    ... /*This code finally compiles as a 'c' code ... One more question....if i don't free up the memory allocated at the end ... it would appear that we are best served by an array ... prime numbers can be represented by an unsigned long int. ...
    (comp.lang.c)
  • Re: Newbie questions
    ... It compiles ... float getLowest(float array, int length); ... float getLowest(float array[], int length) { ... before printing a second prompt. ...
    (comp.lang.c)