# 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. Feb 6, 2011 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. Feb 7, 2011 at 5:51

# 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


# Bash, 27 bytes

echo $[${1//./<<32-++i*8|}]


Try it online!

At the cost of 1 character, this can avoid depending on the state of i:

echo $[i=4,${1//./<<--i*8|}]


Dramatic improvement on existing bash solution (46 characters at time of writing).

• Well seen and clever! By using integer: declare -i i;i="i=4,${1//./<<--i*8|}" will assign integer value to $i! Nice! Nov 7, 2023 at 7:33
• Not shorter variant: echo $[i=3,${1//./<<i--*8|}] Nov 7, 2023 at 7:41

# K (ngn/k), 10 bytes

256/.'"."\


Try it online!

• "."\ split string input on .'s
• .' convert each string to its integer
• 256/ convert from base-256

# 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, 2012 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.

## 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.

# Pxem, Filename: 58 bytes + Content: 0 bytes = 58 bytes.

• Filename (escaped): ._\377\001.+.c.c.!.!.!.i.s._\377\001.+.c.!.!.+.i.s._\377\001.+.!.+.i.s._.+.n
• Content: empty

Problem is that my implementation of Pxem supports literal value up to 255; needed to make greatee values by myself.

## Usage

Via stdin/stdout. Accepts only one input at once.

Try it online!

# Pinecone, 266 bytes

u:IntArray:4;s:"".input;k:IntArray:s.len;k.set:0,-1;n:1
i:0|i<s.len|i:i+1@((s.sub:i,i+1)="."?(k.set:n,i;n:n+1))k.set:n,s.len;i:1|i<n+1|i:i+1@(j:s.sub:((k.get:i-1)+1),(k.get:i)u.set:(i-1),(j.Int))print:((u.get:0)*16777216)+((u.get:1)*65536)+((u.get:2)*256)+(u.get:3)


# Thunno 2, 5 bytes

'./ɠḋ


Try it online!

#### Explanation

'./ɠḋ  '# Implicit input
'./    '# Split it on "."s
ɠḋ   # Convert from base-256
# Implicit output


# Python 3.11+ (no eval/built-in libraries), 47 bytes

c=lambda x:0 .from_bytes(map(int,x.split('.')))


As of 3.11, int.from_bytes implicitly uses big-endian byte order. 0. from_bytes is shorter than int.from_bytes, and works just fine (since from_bytes is a class method, it works the same when called on an instance; the space is needed to avoid 0. being interpreted as a float).

Attempt This Online!