Re: Can this conversion code be simplified?
- From: "Eric Lilja" <mindcooler@xxxxxxxxx>
- Date: 8 Apr 2006 13:16:33 -0700
Joe Wright wrote:
Eric Lilja wrote:
Hello, I have IPv4-numbers in the following format:I don't know yet. When I compile the code you posted I get..
"\\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
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
.
- Follow-Ups:
- Re: Can this conversion code be simplified?
- From: CBFalconer
- Re: Can this conversion code be simplified?
- References:
- Can this conversion code be simplified?
- From: Eric Lilja
- Re: Can this conversion code be simplified?
- From: Joe Wright
- Can this conversion code be simplified?
- Prev by Date: Re: linking error
- Next by Date: Re: restrictions with malloc???
- Previous by thread: Re: Can this conversion code be simplified?
- Next by thread: Re: Can this conversion code be simplified?
- Index(es):
Relevant Pages
|