473,765 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 21464
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 like my last try?

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

section _DATA public align=4 class=DATA use32

Scrivi_numero_i p db "Srivi un internet adr. > " , 0
Errore db "Errore" , 10 , 0
Errore1 db "Errore" , 0
Errore_linea_tr oppo_lunga db "Errore linea troppo lunga" , 0
Numero_naturale _conversione_$u
db "Numero naturale conversione %u" , 10 , 0
Errore_contenut o_linea db "Errore contenuto linea" , 10 , 0

$lf db "%lf" , 0
errno_u_$i db "errno=%i " , 0
$u_$u db " <%u %u> " , 0
$i_p$i_p$i_p$i db " %d.%d.%d.%d%*[^" , 10 , "]" , 0
val db 0

extern _printf , _errno , _sscanf , _fgets
extern _perror , __streams , _isspace
global _main

section _TEXT public align=1 class=CODE use32

; unsigned decarr_to_u(cha r* dove, char* arr)
; s=0c, 4i, 8r, 12b, 16ra, 20dove, 24arr
decarr_to_u:
push ebx
push edx
push esi
push ecx
mov esi, [esp+24]
mov edx, 0
mov eax, 0
mov ecx, 3
..a0:
mov dl, [esi]
..a1:
cmp edx, '9'
jg .e1
cmp edx, '0'
jl .e1
jmp short .a2
..e0:
mov eax, [esp+24]
mov ebx, [esp+20]
mov [ebx], eax
mov eax, 0
jmp short .fn
..e1:
cmp ecx, 3
je .e0
cmp edx, ' '
je .a4
cmp edx, 10
je .a4
cmp edx, '.'
je .a4
jmp short .e0

..a2:
lea ebx, [4*eax+eax]
sub edx, '0'
lea eax, [2*ebx]
add eax, edx
inc esi
dec ecx
jnz .a0

..a3: ; errore +3 digits
cmp ecx, 0
jne .a4
cmp dl, 0
je .a4
mov dl, [esi]
cmp edx, '9'
jg .a4
cmp edx, '0'
jl .a4
jmp short .e0
..a4:
mov edx, [esp+20]
mov [edx], esi
..fn:
pop ecx
pop esi
pop edx
pop ebx
ret

; unsigned hexarr_to_u(cha r* dove, char* arr)
; s=0c, 4i, 8r, 12b, 16ra, 20dove, 24arr
hexarr_to_u:
push ebx
push edx
push esi
push ecx
mov esi, [esp+24]
mov edx, 0
mov eax, 0
mov ecx, 3
cmp byte[esi], '0'
jne .e0
inc esi
cmp byte[esi], 'x'
je .h0
cmp byte[esi], 'X'
je .h0
jmp .e0
..h0:
inc esi
..a0:
mov dl, [esi]
..a1:
cmp edx, 'F'
ja .c0
cmp edx, 'A'
jb .c0
sub edx, 'A'
add edx, 10
jmp short .a2
..c0:
cmp edx, 'f'
ja .c1
cmp edx, 'a'
jb .c1
sub edx, 'a'
add edx, 10
jmp short .a2
..c1:
cmp edx, '9'
jg .e1
cmp edx, '0'
jl .e1
sub edx, '0'
jmp short .a2
..e0:
mov eax, [esp+24]
mov ebx, [esp+20]
mov [ebx], eax
mov eax, 0
jmp short .fn
..e1:
cmp ecx, 3
je .e0
cmp edx, ' '
je .a6
cmp edx, 10
je .a6
cmp edx, '.'
je .a6
jmp short .e0
..a2:
shl eax, 4
inc esi
add eax, edx
dec ecx
jnz .a0

..a3: ; errore +3 digits
cmp ecx, 0
jne .a6
cmp dl, 0
je .a6
mov dl, [esi]
cmp edx, 'F'
jg .a4
cmp edx, 'A'
jl .a4
jmp short .e0
..a4:
cmp edx, 'f'
jg .a5
cmp edx, 'a'
jl .a5
jmp short .e0
..a5:
cmp edx, '9'
jg .a6
cmp edx, '0'
jge .a6
jmp .e0

..a6:
mov edx, [esp+20]
mov [edx], esi
..fn:
pop ecx
pop esi
pop edx
pop ebx
ret
; ip_to_ul(dword* ris, dword* arr)
; k=0k, 4Ra, 8ris, 12arr
ip_to_ul:
push ebp
mov ebp, esp ; @w
sub esp, 32 ; 21..24 <32
push ebx
push ecx
push edx
push esi
push edi
%define @ris ebp+8
%define @arr ebp+12
%define @w ebp+24
mov esi, [@arr]
xor ebx, ebx
jmp short .c1
..c0:
inc esi
..c1:
mov bl, [esi]
push ebx
call _isspace
add esp, 4
cmp eax, 0
jne .c0

cmp bl, 0
jne .c2
..e0:
mov eax, [@ris]
mov dword[eax], 0
jmp .fn
..c2: ; c=1.2.3.4
mov ecx, 4
mov edx, 0
mov ebx, 0
..c3:
lea eax, [@w]
push esi
push eax
call decarr_to_u
add esp, 8
cmp esi, [@w]
jne .c4
lea eax, [@w]
push esi
push eax
call hexarr_to_u
add esp, 8
cmp esi, [@w]
je .e0

..c4:
cmp eax, 255
ja .e0
shl ebx, 8
mov esi, [@w]
add ebx, eax
cmp ecx, 1
je .c5
cmp byte[esi], '.'
jne .e0
jmp short .c6
..c5:
cmp byte[esi], ' '
je .c7
cmp byte[esi], 10
je .c7
jmp short .e0
..c6:
inc esi
dec ecx
jmp short .c3
..c7:
mov eax, [@ris]
mov dword[eax], 1
mov eax, ebx
..fn:
%undef @ris
%undef @arr
%undef @w
pop edi
pop esi
pop edx
pop ecx
pop ebx
mov esp, ebp
pop ebp
ret
; k= 0k, 4Ra, 8@argc, 12@argv
_main: ; foo[256], sp[4], w[4]
push ebp ; 11..266, 267..270, 271..274<512
mov ebp, esp
sub esp, 512
push ebx
push esi
push edi
%define @argc ebp+8
%define @argv ebp+12
%define @foo ebp-266
%define @w ebp-274
mov eax, 0
mov ebx, ebp
..l0:
mov byte[ebx], 0
inc eax
dec ebx
cmp eax, 400
jb .l0
..a0:
push Scrivi_numero_i p
call _printf
add esp, 4
lea eax, [@foo]
push __streams
push 256
push eax
call _fgets
add esp, 12
cmp eax, 0
jne .l4
..l1:
push Errore
call _printf
add esp, 4
jmp .fine
..l2:
push Errore_linea_tr oppo_lunga
call _printf
add esp, 4
jmp .fine
..l3:
push Errore_contenut o_linea
call _printf
add esp, 4
jmp .fine
..l4:
cmp byte[eax+254], 0
jne .l2
lea ebx, [@w]
push eax
push ebx
call ip_to_ul
add esp, 8
cmp dword[@w], 0
je .l3
cmp eax, 0
je .fine
push eax
push Numero_naturale _conversione_$u
call _printf
add esp, 8
jmp .l0
..fine:
mov eax, 0
%undef @argc
%undef @argv
%undef @foo
%undef @w
pop edi
pop esi
pop ebx
mov esp, ebp
pop ebp
ret
Feb 2 '06 #21

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 like my last try?


A few comments (in any language) might help.

--
Regards
Alex McDonald

Feb 2 '06 #22

Alex McDonald 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 like my last try?


A few comments (in any language) might help.


*Please* don't feed the troll...

Thanks

Vladimir

Feb 2 '06 #23
On Thu, 02 Feb 2006 10:52:17 +0100, RSoIsCaIrLiIoA <zz@zz.z> wrote:
cmp dword[@p1], 256
jg .a0
cmp dword[@p2], 256
jg .a0
cmp dword[@p3], 256
jg .a0
cmp dword[@p4], 256
jg .a0


this should be 255 not 256
Feb 2 '06 #24
On Thu, 02 Feb 2006 16:29:58 +0100, RSoIsCaIrLiIoA <zz@zz.z> 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 like my last try?


i add octal (i don't know if it is right it is the first time i write
something in 'octal')

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

section _DATA public align=4 class=DATA use32

Scrivi_numero_i p db "Srivi un internet adr. > " , 0
Errore db "Errore" , 10 , 0
Errore1 db "Errore" , 0
Errore_linea_tr oppo_lunga db "Errore linea troppo lunga" , 0
Numero_naturale _conversione_$u
db "Numero naturale conversione %u" , 10 , 0
Errore_contenut o_linea db "Errore contenuto linea" , 10 , 0

$lf db "%lf" , 0
errno_u_$i db "errno=%i " , 0
$u_$u db " <%u %u> " , 0
$i_p$i_p$i_p$i db " %d.%d.%d.%d%*[^" , 10 , "]" , 0
val db 0

extern _printf , _errno , _sscanf , _fgets
extern _perror , __streams , _isspace
global _main

section _TEXT public align=1 class=CODE use32
; unsigned otto_to_u(char* dove, char* arr)
; s=0c, 4i, 8r, 12b, 16ra, 20dove, 24arr
otto_to_u:
push ebx
push edx
push esi
push ecx
mov esi, [esp+24]
mov edx, 0
mov eax, 0
mov ecx, 3
cmp byte[esi], '0'
jne .e0
inc esi
cmp byte[esi], ' '
jne .b1
..b0:
mov eax, 0
jmp .a4
..b1:
cmp byte[esi], 10
je .b0
cmp byte[esi], 0
je .b0
cmp byte[esi], '.'
je .b0
..a0:
mov dl, [esi]
..a1:
cmp edx, '7'
jg .e1
cmp edx, '0'
jl .e1
jmp short .a2
..e0:
mov eax, [esp+24]
mov ebx, [esp+20]
mov [ebx], eax
mov eax, 0
jmp short .fn
..e1:
cmp ecx, 3
je .e0
cmp edx, ' '
je .a4
cmp edx, 10
je .a4
cmp edx, '.'
je .a4
cmp edx, 0
je .a4
jmp short .e0

..a2:
shl eax, 3
sub edx, '0'
add eax, edx
inc esi
dec ecx
jnz .a0

..a3: ; errore +3 digits
cmp ecx, 0
jne .a4
cmp dl, 0
je .a4
mov dl, [esi]
cmp edx, '7'
jg .a4
cmp edx, '0'
jl .a4
jmp short .e0
..a4:
mov edx, [esp+20]
mov [edx], esi
..fn:
pop ecx
pop esi
pop edx
pop ebx
ret

; unsigned decarr_to_u(cha r* dove, char* arr)
; s=0c, 4i, 8r, 12b, 16ra, 20dove, 24arr
decarr_to_u:
push ebx
push edx
push esi
push ecx
mov esi, [esp+24]
mov edx, 0
mov eax, 0
mov ecx, 3
..a0:
mov dl, [esi]
..a1:
cmp edx, '9'
jg .e1
cmp edx, '0'
jl .e1
jmp short .a2
..e0:
mov eax, [esp+24]
mov ebx, [esp+20]
mov [ebx], eax
mov eax, 0
jmp short .fn
..e1:
cmp ecx, 3
je .e0
cmp edx, ' '
je .a4
cmp edx, 0
je .a4
cmp edx, 10
je .a4
cmp edx, '.'
je .a4
jmp short .e0

..a2:
lea ebx, [4*eax+eax]
sub edx, '0'
lea eax, [2*ebx]
add eax, edx
inc esi
dec ecx
jnz .a0

..a3: ; errore +3 digits
cmp ecx, 0
jne .a4
cmp dl, 0
je .a4
mov dl, [esi]
cmp edx, '9'
jg .a4
cmp edx, '0'
jl .a4
jmp short .e0
..a4:
mov edx, [esp+20]
mov [edx], esi
..fn:
pop ecx
pop esi
pop edx
pop ebx
ret

; unsigned hexarr_to_u(cha r* dove, char* arr)
; s=0c, 4i, 8r, 12b, 16ra, 20dove, 24arr
hexarr_to_u:
push ebx
push edx
push esi
push ecx
mov esi, [esp+24]
mov edx, 0
mov eax, 0
mov ecx, 3
cmp byte[esi], '0'
jne .e0
inc esi
cmp byte[esi], 'x'
je .h0
cmp byte[esi], 'X'
je .h0
jmp .e0
..h0:
inc esi
..a0:
mov dl, [esi]
..a1:
cmp edx, 'F'
ja .c0
cmp edx, 'A'
jb .c0
sub edx, 'A'
add edx, 10
jmp short .a2
..c0:
cmp edx, 'f'
ja .c1
cmp edx, 'a'
jb .c1
sub edx, 'a'
add edx, 10
jmp short .a2
..c1:
cmp edx, '9'
jg .e1
cmp edx, '0'
jl .e1
sub edx, '0'
jmp short .a2
..e0:
mov eax, [esp+24]
mov ebx, [esp+20]
mov [ebx], eax
mov eax, 0
jmp .fn
..e1:
cmp ecx, 3
je .e0
cmp edx, ' '
je .a6
cmp edx, 0
je .a6
cmp edx, 10
je .a6
cmp edx, '.'
je .a6
jmp short .e0
..a2:
shl eax, 4
inc esi
add eax, edx
dec ecx
jnz .a0

..a3: ; errore +3 digits
cmp ecx, 0
jne .a6
cmp dl, 0
je .a6
mov dl, [esi]
cmp edx, 'F'
jg .a4
cmp edx, 'A'
jl .a4
jmp short .e0
..a4:
cmp edx, 'f'
jg .a5
cmp edx, 'a'
jl .a5
jmp short .e0
..a5:
cmp edx, '9'
jg .a6
cmp edx, '0'
jge .a6
jmp .e0

..a6:
mov edx, [esp+20]
mov [edx], esi
..fn:
pop ecx
pop esi
pop edx
pop ebx
ret
; ip_to_ul(dword* ris, dword* arr)
; k=0k, 4Ra, 8ris, 12arr
ip_to_ul:
push ebp
mov ebp, esp ; @w
sub esp, 32 ; 21..24 <32
push ebx
push ecx
push edx
push esi
push edi
%define @ris ebp+8
%define @arr ebp+12
%define @w ebp+24
mov esi, [@arr]
xor ebx, ebx
jmp short .c1
..c0:
inc esi
..c1:
mov bl, [esi]
push ebx
call _isspace
add esp, 4
cmp eax, 0
jne .c0

cmp bl, 0
jne .c2
..e0:
mov eax, [@ris]
mov dword[eax], 0
jmp .fn
..c2: ; c=1.2.3.4
mov ecx, 4
mov edx, 0
mov ebx, 0
..c3:
cmp byte[esi], '0'
jne .b0
cmp byte[esi+1], 'x'
je .a1
cmp byte[esi+1], 'X'
jne .a2
..a1:
lea eax, [@w]
push esi
push eax
call hexarr_to_u
add esp, 8
jmp short .c4
..a2:
lea eax, [@w]
push esi
push eax
call otto_to_u
add esp, 8
jmp short .c4
..b0:
lea eax, [@w]
push esi
push eax
call decarr_to_u
add esp, 8
..c4:
cmp esi, [@w]
je .e0
cmp eax, 255
ja .e0
shl ebx, 8
mov esi, [@w]
add ebx, eax
cmp ecx, 1
je .c5
cmp byte[esi], '.'
jne .e0
jmp short .c6
..c5:
cmp byte[esi], ' '
je .c7
cmp byte[esi], 10
je .c7
jmp .e0
..c6:
inc esi
dec ecx
jmp short .c3
..c7:
mov eax, [@ris]
mov dword[eax], 1
mov eax, ebx
..fn:
%undef @ris
%undef @arr
%undef @w
pop edi
pop esi
pop edx
pop ecx
pop ebx
mov esp, ebp
pop ebp
ret
; k= 0k, 4Ra, 8@argc, 12@argv
_main: ; foo[256], sp[4], w[4]

push ebp ; 11..266, 267..270, 271..274<512
mov ebp, esp
sub esp, 512
push ebx
push esi
push edi
%define @argc ebp+8
%define @argv ebp+12
%define @foo ebp-266
%define @w ebp-274
mov eax, 0
mov ebx, ebp
..l0:
mov byte[ebx], 0
inc eax
dec ebx
cmp eax, 400
jb .l0
..a0:
push Scrivi_numero_i p
call _printf
add esp, 4
lea eax, [@foo]
push __streams
push 256
push eax
call _fgets
add esp, 12
cmp eax, 0
jne .l4
..l1:
push Errore
call _printf
add esp, 4
jmp .fine
..l2:
push Errore_linea_tr oppo_lunga
call _printf
add esp, 4
jmp .fine
..l3:
push Errore_contenut o_linea
call _printf
add esp, 4
jmp .fine
..l4:
cmp byte[eax+254], 0
jne .l2
lea ebx, [@w]
push eax
push ebx
call ip_to_ul
add esp, 8
cmp dword[@w], 0
je .l3
cmp eax, 0
je .fine
push eax
push Numero_naturale _conversione_$u
call _printf
add esp, 8
jmp .l0
..fine:
mov eax, 0
%undef @argc
%undef @argv
%undef @foo
%undef @w
pop edi
pop esi
pop ebx
mov esp, ebp
pop ebp
ret
Feb 2 '06 #25
On Thu, 02 Feb 2006 16:29:58 +0100, RSoIsCaIrLiIoA <zz@zz.z> wrote:
decarr_to_u:
push ebx
push edx
push esi
push ecx .e1:
cmp ecx, 3
je .e0
cmp edx, ' '
je .a4
cmp edx, 10
je .a4
cmp edx, '.'
je .a4
jmp short .e0


here i have to add in here all routines
cmp edx, 0
je .a4
Feb 2 '06 #26
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 like my last try?

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?

00000000: be 00000051 move.l #text,r5
00000005: e8 00000005 bsr.l convert ; result in eax
0000000a: e9 00000051 br.l exit

convert:
0000000f: b3 03 move.b #3,r3
00000011: 31 d2 _30: eor.l r1,r1
00000013: 31 c0 _20: eor.l r0,r0
00000015: 8a 06 move.b (r5),r0
00000017: 46 inc.l r5
00000018: 08 c0 or.b r0,r0
0000001a: 74 25 beq.b _100
0000001c: 3c 2e cmp.b #'.',r0
0000001e: 74 17 beq.b _10
00000020: 2c 30 sub.b #'0',r0
00000022: 72 2b bcs.b _error
00000024: 3c 09 cmp.b #9,r0
00000026: 77 27 bhi.b _error
00000028: 6b d2 0a mulsq.l #10,r1,r1
0000002b: 01 c2 add.l r0,r1
0000002d: 81 fa 000000ff cmp.l #255,r1
00000033: 77 1a bhi.b _error
00000035: eb dc br.b _20
00000037: fe cb _10: dec.b r3
00000039: 72 14 bcs.b _error
0000003b: 0f ac d1 08 dsr.l #8,r1>r2
0000003f: eb d0 br.b _30
00000041: 08 db _100: or.b r3,r3
00000043: 75 0a bne.b _error
00000045: 0f ac d1 08 dsr.l #8,r1>r2
00000049: 0f c9 bswap.l r2
0000004b: 89 c8 move.l r2,r0
0000004d: f8 bclr.l #0,sr
0000004e: c3 rts.l
0000004f: f9 _error: bset.l #0,sr
00000050: c3 rts.l

00000051: 31 33 37 2e 31 39
00000057: 33 2e 36 34 2e 31
0000005d: 33 30 00 text: dc.b "137.193.64.130 ",0
Feb 2 '06 #27
Herbert Kleebauer <kl**@unibwm.de > writes:
RSoIsCaIrLiIoA wrote:

[snip]
do you like my last try?


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?

00000000: be 00000051 move.l #text,r5
00000005: e8 00000005 bsr.l convert ; result in eax
0000000a: e9 00000051 br.l exit

[snip]

To the folks in alt.lang.asm:

"RSoIsCaIrLiIoA " is a troll, deliberately posting assembly language to
comp.lang.c. I don't know whether what he/she writes makes sense in
the context of alt.lang.asm, but if you post a followup, *please* drop
the cross-post to comp.lang.c.

Thank you.

--
Keith Thompson (The_Other_Keit h) 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.
Feb 2 '06 #28
On Thu, 02 Feb 2006 16:29:58 +0100, in comp.lang.c , RSoIsCaIrLiIoA
<zz@zz.z> 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 like my last try?


I don't like any of your tries - you're posting what looks like some
form of assembler to comp.lang.c. Stop that please.
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Feb 2 '06 #29
Mark McIntyre wrote:
On Thu, 02 Feb 2006 16:29:58 +0100, in comp.lang.c , RSoIsCaIrLiIoA
<zz@zz.z> 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 like my last try?

I don't like any of your tries - you're posting what looks like some
form of assembler to comp.lang.c. Stop that please.
Mark McIntyre


I hate to be late. Assuming this was ever a valid question, how about
this? Is there a better way?

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

unsigned long
iptoul(char *ip) {
unsigned long ul = 0, t;
char *p;

do {
t = strtoul(ip, &p, 0);
ul = ul * 256 + t;
ip = p + 1;
} while (*p == '.');

return ul;
}

char *
ultoip(unsigned long ul) {
static char ip[16];
char t[16];
char *tp;
unsigned long tl;
ip[0] = '\0';

do {
tp = t;
tl = ul % 256;
ul /= 256;
if (ul) *t = '.', ++tp;
sprintf(tp, "%lu", tl);
strcat(t, ip);
strcpy(ip, t);
} while (ul);

return ip;
}

int
main(int argc, char *argv[]) {
char *ip;
unsigned long ul;
if (argc > 1)
ip = argv[1];
else
ip = "1.2.3.4";
ul = iptoul(ip);
printf("%lu, %08x\n", ul, (unsigned)ul);
puts(ultoip(ul) );
return 0;
}

The theory is that really well written stuff doesn't need comments. :-)

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Feb 3 '06 #30

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

Similar topics

4
5432
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 typedef struct Rec { unsigned char msg; unsigned long len;
7
2155
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 that demonstrates the issue: --- snip --- #include <unistd.h> int main() { char buf;
2
3123
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 I am only interested data from one client, I know that client's IP address in form of 127.0.0.1 etc. I think I need convert that IP into IPaddress then can compare with
1
2083
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?
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10164
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5277
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.