# IPv4 Integer Conversion Function

Write the shortest function to convert an IP address into it's integer representation and output it as an integer.

To change an IPv4 address to it's integer representation, the following calculation is required:

• Break the IP address into it's four octets.
• (Octet1 * 16777216) + (Octet2 * 65536) + (Octet3 * 256) + (Octet4)

Sample Input

192.168.1.1           10.10.104.36           8.8.8.8


Sample Output

3232235777            168454180              134744072

• I think this would be better if there was a restriction in place prohibiting a language's built-in functions. – Nathan Osman Feb 6 '11 at 20:04
• @George - Yea, it would have been, but people had already done it before the I could put that in - I honestly didn't think about it. – Kyle Rosendo Feb 7 '11 at 5:51

# Powershell - 53

Variation on Ty Auvil's answer, which is a variation on Joey's answer:

%{([ipaddress]($_.split('.')[3..0]-join'.')).address} PS C:\> '192.168.1.1' | %{([ipaddress]($_.split('.')[3..0]-join'.')).address}
3232235777
PS C:\> '10.10.104.36' | %{([ipaddress]($_.split('.')[3..0]-join'.')).address} 168454180 PS C:\> '8.8.8.8' | %{([ipaddress]($_.split('.')[3..0]-join'.')).address}
134744072


I would have just made a suggestion in the comments, but not enough rep.

ASM - 98 byte executable (WinXP command shell), about 485 characters

    mov si,82h
mov di,10
mov bp,l3
fldz
push l2
push l0
push l0
push l0
l0:xor ax,ax
xor cx,cx
mov [bp+di],ax
mul di
mov cx,ax
lodsb
sub al,'0'
jnc l1
fild w[bp]
fmulp
fild w[bp+di]
ret
l2:fbstp [si]
mov bx,di
mov di,bp
l4:dec bx
jz l5
mov al,[si+bx-1]
aam 16
xchg al,ah
stosw
jmp l4
l5:mov al,'$' stosb lea di,[bp-1] l6:inc di cmp b[di],'0' je l6 l7:cmp b[di],al jne l8 dec di l8:mov dx,di mov ah,9 int 21h ret l3:dw 256  ## C (91) Not going to win anyway, so I tried to be a bit creative. Tested on 32-bit GCC 4.4.3. main(a,c,b)char**c,*b;{c=c[1];for(b=&a+1;c=strtok(c,".");c=0)*--b=atoi(c);printf("%u",a);}  ## Scala 59 chars: def f(i:String)=(0L/:i.split("\\.").map(_.toInt))(_*256+_)  ## Perl with builtins (35): unpack"N",pack"C4",split/\./,shift;  ## Perl without builtins (42): split/\./,shift;$s+=$_<<@_*8while$_=shift;


## Python (No eval) - 67

c=lambda x:long(''.join(["%02X"%long(i) for i in x.split('.')]),16)

• can you not shorten it a few more characters by using int() instead of long()? – Wug Sep 24 '12 at 6:58

# Mathematica, 53 chars

f=FromDigits;f[f/@StringSplit[InputString[],"."],256]


# Jelly, 13 bytes

4Ḷ⁹*U
ṣ”.V×¢S


Try it online!

I don't think Jelly has a builtin for this. At least, I couldn't find one.

# J, 21 bytes

[:256&#.[:".;._1'.'&,


Try it online!

## explanation

[: 256&#. [: ".;._1 '.'&,
[: 256&#.                   NB. assuming a base 256 list,
NB. convert the following to decimal:
'.'&,   NB. the input with a dot appended
[: ".;._1         NB. split at dots and converted to ints


# APL(NARS), 16 chars, 32 bytes

{256⊥⍎¨⍵⊂⍨⍵≠'.'}


test

  f←{256⊥⍎¨⍵⊂⍨⍵≠'.'}
f¨'192.168.1.1' '10.10.104.36' '8.8.8.8'
3232235777 168454180 134744072


# Java 8, 83 bytes

i->{long u=0;for(int j=0;j<4;)u+=new Long(i.split("\\.")[j])<<(24-8*j++);return u;}

Try it online!

Could have saved 10 bytes by moving the initialisation of u and j "up" a level but I'm not entirely sure that's allowed so I went for the safe version of my solution.

# Jelly, 6 bytes

ṣ”.Vḅ⁹


Try it online!

### How it works

ṣ”.Vḅ⁹  Main link (monad). Input: string
ṣ”.     Split at dots
V    Convert to numbers
ḅ⁹  Convert base 256 to integer


## Burlesque - 11 bytes

'.;;ri256ug

'.;;           split by .
256ug    undigits base 256


Try it online.

# Kotlin, 50 bytes

{it.split(".").fold(0L){a,v->v.toInt()+(a shl 8)}}


Try it online!

# Japt, 10 4 bytes

nG²o


Try it

 G       :16
²      :Squared
o     :Range [0,G²)
n        :Convert input string from that base to base-10 integer


# min, 53 bytes

"." split 3 :a (int 256 a pow * a 1 - @a int) map sum


Takes IP address as string on stack. Leaves the converted number on top of stack.