473,385 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Convert an ip address to long value

I am trying to write a function to convert an ipv4 address that is held
in the string char *ip to its long value equivalent. Here is what I
have right now, but I can't seem to get it to work.

#include <string.h>
#include <stdio.h>

/* Convert an ipv4 address to long integer */
/* "192.168.1.1" --> 3232235777 */
unsigned long iptol(char *ip){
unsigned char o1,o2,o3,o4; /* The 4 ocets */
char tmp[13] = "000000000000\0";
short i = 11; /* Current Index in tmp */
short j = (strlen(ip) - 1);
do {
if ((ip[--j] == '.')){
i -= (i % 3);
}
else {
tmp[--i] = ip[j];
}
} while (i > -1);
o1 = (tmp[0] * 100) + (tmp[1] * 10) + tmp[2];
o2 = (tmp[3] * 100) + (tmp[4] * 10) + tmp[5];
o3 = (tmp[6] * 100) + (tmp[7] * 10) + tmp[8];
o4 = (tmp[9] * 100) + (tmp[10] * 10) + tmp[11];
return (o1 * 16777216) + (o2 * 65536) + (o3 * 256) + o4;
}

int main(void){
char *ip = "124.15.22.102";
iptol(ip);
}

Any suggestions? Maybe there is a better way to do this?

-kyle

Jan 31 '06
65 21272
RSoIsCaIrLiIoA wrote:
Whatever possessed you to post this stuff

; nasmw -f obj this_file.asm
; bcc32 this_file.obj

section _DATA public align=4 class=DATA use32

Scrivi_numero_ip db "Srivi un internet adr. > " , 0


[etc. for 489 lines]

to <news:comp.lang.c>? It may well be that <news:alt.lang.asm> welcomes
such longish postings of code, but please remove CLC from your
crossposting. Your post is obviously off-topic in CLC and unwelcome.
{FWUPs set]
Apr 25 '06 #51
Martin Ambuhl wrote:
RSoIsCaIrLiIoA wrote:

Whatever possessed you to post this stuff

; nasmw -f obj this_file.asm
; bcc32 this_file.obj

section _DATA public align=4 class=DATA use32

Scrivi_numero_ip db "Srivi un internet adr. > " , 0


[etc. for 489 lines]

to <news:comp.lang.c>? It may well be that <news:alt.lang.asm>
welcomes such longish postings of code, but please remove CLC
from your crossposting. Your post is obviously off-topic in CLC
and unwelcome. {FWUPs set]


It's all in the sig.

--

+-------------------+ .:\:\:/:/:.
| PLEASE DO NOT F :.:\:\:/:/:.:
| FEED THE TROLLS | :=.' - - '.=:
| | '=(\ 9 9 /)='
| Thank you, | ( (_) )
| Management | /`-vvv-'\
+-------------------+ / \
| | @@@ / /|,,,,,|\ \
| | @@@ /_// /^\ \\_\
@x@@x@ | | |/ WW( ( ) )WW
\||||/ | | \| __\,,\ /,,/__
\||/ | | | jgs (______Y______)
/\/\/\/\/\/\/\/\//\/\\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
================================================== ============

fix (vb.): 1. to paper over, obscure, hide from public view; 2.
to work around, in a way that produces unintended consequences
that are worse than the original problem. Usage: "Windows ME
fixes many of the shortcomings of Windows 98 SE". - Hutchison
Apr 25 '06 #52
In alt.lang.asm RSoIsCaIrLiIoA <zz@zz.z> wrote in part:
On Sun, 05 Feb 2006 15:51:26 +0100, RSoIsCaIrLiIoA <zz@zz.z> wrote:
************************************************** **************
On 02 Feb 2006 20:56:38, Herbert Kleebauer <kl**@unibwm.de> wrote:
RSoIsCaIrLiIoA wrote:
On 31 Jan 2006 12:27:07 -0800, "kyle.tk" <ky*****@gmail.com> wrote:
>I am trying to write a function to convert an ipv4 address that is held
>in the string char *ip to its long value equivalent. Here is what I
>have right now, but I can't seem to get it to work.

Do you want to convert a string like "137.193.64.130" into a 32 bit value
stored in a register (eax)? And for this you need more than 350 line of
code?
in the origin was

"ping 192.0xb2.031.249"

it is for convert ip number like above; the number can be after each
point hexadecimal, decimal or octotal. you are right, i have not
'reduced' it; you are not the only one that like
small programs :))))))))


i have try to reduce but with no success [snippage]:

$i_p$i_p$i_p$i db " %d.%d.%d.%d%*[^" , 10 , "]" , 0 extern _printf , _errno , _sscanf , _fgets

Is this some sort of homework assignment?
If you're going to use sscanf(), why not use the %i
conversion that will do the hex, octal & decimal?

Something like [untested]:

call_entry:
push d
push c
push b
push a
push fmt
push input_string
call sscanf

add esp, 20
mov eax, [a]
shl eax, 4
add eax, [b]
shl eax, 4
add eax, [c]
shl eax, 4
add eax, [d]
ret

fmt db " %i.%i.%i.%i"

Add error checking to taste (not more than 7 instructions) :)

-- Robert

Apr 25 '06 #53

Robert Redelmeier wrote:
In alt.lang.asm RSoIsCaIrLiIoA <zz@zz.z> wrote in part:
On Sun, 05 Feb 2006 15:51:26 +0100, RSoIsCaIrLiIoA <zz@zz.z> wrote:
************************************************** **************


--

+-------------------+ .:\:\:/:/:.
| PLEASE DO NOT F :.:\:\:/:/:.:
| FEED THE TROLLS | :=.' - - '.=:
| | '=(\ 9 9 /)='
| Thank you, | ( (_) )
| Management | /`-vvv-'\
+-------------------+ / \
| | @@@ / /|,,,,,|\ \
| | @@@ /_// /^\ \\_\
@x@@x@ | | |/ WW( ( ) )WW
\||||/ | | \| __\,,\ /,,/__
\||/ | | | jgs (______Y______)
/\/\/\/\/\/\/\/\//\/\\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
================================================== ============

Apr 25 '06 #54
Vladimir Oka wrote:
Robert Redelmeier wrote:
In alt.lang.asm RSoIsCaIrLiIoA <zz@zz.z> wrote in part:
On Sun, 05 Feb 2006 15:51:26 +0100, RSoIsCaIrLiIoA <zz@zz.z> wrote:
************************************************** **************


--

+-------------------+ .:\:\:/:/:.
| PLEASE DO NOT F :.:\:\:/:/:.:
| FEED THE TROLLS | :=.' - - '.=:
| | '=(\ 9 9 /)='
| Thank you, | ( (_) )
| Management | /`-vvv-'\
+-------------------+ / \
| | @@@ / /|,,,,,|\ \
| | @@@ /_// /^\ \\_\
@x@@x@ | | |/ WW( ( ) )WW
\||||/ | | \| __\,,\ /,,/__
\||/ | | | jgs (______Y______)
/\/\/\/\/\/\/\/\//\/\\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
================================================== ============

[OT]
I'm pretty sure that trolls practise cannibalism. So this warning is
likely just as much food as any shorter reply.
Apr 25 '06 #55
In alt.lang.asm Vladimir Oka <no****@btopenworld.com> wrote in part:
| PLEASE DO NOT F :.:\:\:/:/:.:
| FEED THE TROLLS | :=.' - - '.=:


What do you consider a troll?
I suspect we have very different definitions.

-- Robert

Apr 25 '06 #56
Robert Redelmeier <re****@ev1.net.invalid> writes:
In alt.lang.asm Vladimir Oka <no****@btopenworld.com> wrote in part:
| PLEASE DO NOT F :.:\:\:/:/:.:
| FEED THE TROLLS | :=.' - - '.=:


What do you consider a troll?
I suspect we have very different definitions.


One example: someone who posts large amounts of assembly code to
comp.lang.c.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 25 '06 #57
In alt.lang.asm Keith Thompson <ks***@mib.org> wrote in part:
Robert Redelmeier <re****@ev1.net.invalid> writes:
In alt.lang.asm Vladimir Oka <no****@btopenworld.com> wrote in part:
| PLEASE DO NOT F :.:\:\:/:/:.:
| FEED THE TROLLS | :=.' - - '.=:


What do you consider a troll?
I suspect we have very different definitions.


One example: someone who posts large amounts of assembly
code to comp.lang.c.


Poor assembly, I would add.

But that is _not_ troll behaviour in ALA,
where I read (see quote line).

-- Robert
Apr 25 '06 #58
Robert Redelmeier wrote:
In alt.lang.asm Keith Thompson <ks***@mib.org> wrote in part:
Robert Redelmeier <re****@ev1.net.invalid> writes:
In alt.lang.asm Vladimir Oka <no****@btopenworld.com> wrote in part:

| PLEASE DO NOT F :.:\:\:/:/:.:
| FEED THE TROLLS | :=.' - - '.=:

What do you consider a troll?
I suspect we have very different definitions.


One example: someone who posts large amounts of assembly
code to comp.lang.c.

Poor assembly, I would add.

But that is _not_ troll behaviour in ALA,
where I read (see quote line).

Just look back through the OP's postings to c.l.c and form your own opinion.

--
Ian Collins.
Apr 25 '06 #59
In alt.lang.asm Ian Collins <ia******@hotmail.com> wrote in part:
Robert Redelmeier wrote:
Poor assembly, I would add.

But that is _not_ troll behaviour in ALA,
where I read (see quote line).

Just look back through the OP's postings to c.l.c and form
your own opinion.


I could, but this would require me to understand CLC.

-- Robert
Apr 25 '06 #60
Robert Redelmeier wrote:
In alt.lang.asm Ian Collins <ia******@hotmail.com> wrote in part:
Robert Redelmeier wrote:
Poor assembly, I would add.

But that is _not_ troll behaviour in ALA,
where I read (see quote line).

Just look back through the OP's postings to c.l.c and form
your own opinion.


I could, but this would require me to understand CLC.


It only requires that you understand that assembler is not C and
comp.lang.c is about C.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Apr 25 '06 #61
In alt.lang.asm Flash Gordon <sp**@flash-gordon.me.uk> wrote in part:
Robert Redelmeier wrote:
In alt.lang.asm Ian Collins <ia******@hotmail.com> wrote in part:
Robert Redelmeier wrote:
Poor assembly, I would add.

But that is _not_ troll behaviour in ALA,
where I read (see quote line).

Just look back through the OP's postings to c.l.c and form
your own opinion.


I could, but this would require me to understand CLC.


It only requires that you understand that assembler is not
C and comp.lang.c is about C.


I already understand this, but my forming an opinion of
trollishness requires considerably more. In particular,
I had no idea that assembly was that anathma to CLC.
There's lots of `c` in ALA.

-- Robert

Apr 26 '06 #62
Robert Redelmeier <re****@ev1.net.invalid> writes:
In alt.lang.asm Flash Gordon <sp**@flash-gordon.me.uk> wrote in part:
Robert Redelmeier wrote:
In alt.lang.asm Ian Collins <ia******@hotmail.com> wrote in part:
Robert Redelmeier wrote:
> Poor assembly, I would add.
>
> But that is _not_ troll behaviour in ALA,
> where I read (see quote line).
>
Just look back through the OP's postings to c.l.c and form
your own opinion.

I could, but this would require me to understand CLC.


It only requires that you understand that assembler is not
C and comp.lang.c is about C.


I already understand this, but my forming an opinion of
trollishness requires considerably more. In particular,
I had no idea that assembly was that anathma to CLC.
There's lots of `c` in ALA.


I wouldn't use the word "anathema", but it's certainly considered
off-topic. comp.lang.c is stricter about topicality than most
newsgroups. We try very hard to stick to the C language as defined by
the ISO standards (or by earlier documents, such as K&R1, in
historical discussions).

As for RoSsIaCrIiLoIA's trollishness or lack thereof, by all means
feel free to form your own opinion in any way you like.

If you're interested in learning more about comp.lang.c,
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c> is a good
introduction. (The comp.lang.c FAQ, <http://www.c-faq.com/>, mostly
covers the language, not the dynamics of the newsgroup.)

If you're *not* interested in learning more about comp.lang.c, that's
perfectly fine. But if you're not going to discuss either the C
language or the comp.lang.c newsgroup (meta-discussions are generally
considered topical), please consider dropping comp.lang.c from the
Newsgroups header. (I'm not tell you to go away, just offering some
insight on the group.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 26 '06 #63
Robert Redelmeier opined:
In alt.lang.asm Vladimir Oka <no****@btopenworld.com> wrote in part:
| PLEASE DO NOT F :.:\:\:/:/:.:
| FEED THE TROLLS | :=.' - - '.=:


What do you consider a troll?


I think others have explained this probably better than I would.

I'll only add that I failed to notice alt.lang.asm in the header. Had I
noticed it, I might have qualified my statement.

--
There are no threads in a.b.p.erotica, so there's no gain in using a
threaded news reader.
(Unknown source)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

Apr 26 '06 #64
In alt.lang.asm Keith Thompson <ks***@mib.org> wrote in part:
I wouldn't use the word "anathema", but it's certainly considered
off-topic. comp.lang.c is stricter about topicality than
most newsgroups. We try very hard to stick to the C language
as defined by the ISO standards (or by earlier documents,
such as K&R1, in historical discussions).
A discussion of only what is, rather than what could or should
be in `c` sounds like a elaborate RTFM! And if you're going
to discuss potential features, then why forbid mention of
languages that incorporate those features? You'd be hobbling
debate in favor of the status-quo.

Speaking of strictness, AFAIK many c's allow inline asm.
So asm is included in `c`?
But if you're not going to discuss either the C language or
the comp.lang.c newsgroup (meta-discussions are generally
considered topical), please consider dropping comp.lang.c
from the Newsgroups header. (I'm not tell you to go away,
just offering some insight on the group.)


This brings up a more general issue: How should one deal with
posts that are on-topic for one of the cross-posted groups, but
potentially abusive of others? It isn't always easy to tell.
Know all the groups? Do you know ALA?

Replying only to one group is rude since it likely will miss
the OP. Setting Follow-Ups is possible (if respected at all),
but that forks discussion and people may never see posts they
would otherwise find interesting.

False-positives are a big concern of mine: I'd rather deal with
100-1000 junk posts/emails than miss a single interesting one.
Of course, other people will set their own S/N tolerance.
Quite possibly higher. But I will not subject myself to
"prior-restraint" or participate in a race-to-politeness.

-- Robert


Apr 26 '06 #65
Robert Redelmeier <re****@ev1.net.invalid> writes:
In alt.lang.asm Keith Thompson <ks***@mib.org> wrote in part:
I wouldn't use the word "anathema", but it's certainly considered
off-topic. comp.lang.c is stricter about topicality than
most newsgroups. We try very hard to stick to the C language
as defined by the ISO standards (or by earlier documents,
such as K&R1, in historical discussions).
A discussion of only what is, rather than what could or should
be in `c` sounds like a elaborate RTFM!


Standard C is big enough to provide more than enough traffic for this
newsgroup. For example, we get a lot of discussions of the form,
"Here's my C code that's supposed to do such-and-such, but it does
this instead; what's the problem?"
And if you're going
to discuss potential features, then why forbid mention of
languages that incorporate those features? You'd be hobbling
debate in favor of the status-quo.
The status quo is what we discuss. There's a separate newsgroup,
comp.std.c, for discussing the C standard document (as opposed to the
language defined by that document), including proposals for changes in
future versions.
Speaking of strictness, AFAIK many c's allow inline asm.
So asm is included in `c`?
No, it's an extension. The C standard allows implementations to
provide extensions (as long as they don't break any strictly
conforming code); the details of any such extensions can be discussed
in a system-specific or compiler-specific newsgroup.
But if you're not going to discuss either the C language or
the comp.lang.c newsgroup (meta-discussions are generally
considered topical), please consider dropping comp.lang.c
from the Newsgroups header. (I'm not tell you to go away,
just offering some insight on the group.)


This brings up a more general issue: How should one deal with
posts that are on-topic for one of the cross-posted groups, but
potentially abusive of others? It isn't always easy to tell.
Know all the groups? Do you know ALA?


No, I'm not familiar with alt.lang.asm; I'm not sure if my news server
even carries it (and I haven't written assembly language in many
years).

The original cross-post was almost certainly inappropriate. Someone
posting to comp.lang.c and alt.lang.asm should be familiar with both
groups, and hardly anything can be topical in both. Possibly someone
could be looking for a solution either in C or in assembly language;
in that case, the poster should ask people to post followups only to
the appropriate newsgroup.
Replying only to one group is rude since it likely will miss
the OP. Setting Follow-Ups is possible (if respected at all),
but that forks discussion and people may never see posts they
would otherwise find interesting.
The OP should be following both newsgroups.
False-positives are a big concern of mine: I'd rather deal with
100-1000 junk posts/emails than miss a single interesting one.
Of course, other people will set their own S/N tolerance.
Quite possibly higher. But I will not subject myself to
"prior-restraint" or participate in a race-to-politeness.


Your tolerance for junk is *much* higher than mine.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 26 '06 #66

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: jagmeena | last post by:
Hello, I am sure this problem has been addressed before, however, I could'nt get a suitable solution to my problem. Hence I am posting here. Thanks a lot for all your help. The code I have is ...
7
by: bryan | last post by:
I think I'm missing something fundamental here. I'm trying to set an unsigned long value via a u_long pointer, however it coredumps everytime I get to that instruction. Here is a sample program...
2
by: Ryan Liu | last post by:
Hi, Can someone tell me how to calculate an IPaddress's long value? I have an application which lisiten on a port using UDP protocol. There could be multiple client sendind UDP data to it and...
1
by: benfly08 | last post by:
Hi, guys. I have a Long value like 12345678, but i want to format it as 12,345,678 and then convert it into String. I can't find out the way to format a long value like that. Any Hints?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.