17
\$\begingroup\$

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
\$\endgroup\$
  • 2
    \$\begingroup\$ I think this would be better if there was a restriction in place prohibiting a language's built-in functions. \$\endgroup\$ – Nathan Osman Feb 6 '11 at 20:04
  • \$\begingroup\$ @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. \$\endgroup\$ – Kyle Rosendo Feb 7 '11 at 5:51

46 Answers 46

1
2
0
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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

Assemble using A86. Badly formed IP addresses generate undefined output.

    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
 l1:add ax,cx
    mov [bp+di],ax
    mul di
    mov cx,ax
    lodsb
    sub al,'0'
    jnc l1
    fild w[bp]
    fmulp
    fild w[bp+di]
    faddp  
    ret
 l2:fbstp [si]
    mov bx,di
    mov di,bp
 l4:dec bx
    jz l5
    mov al,[si+bx-1]
    aam 16
    add ax,'00'
    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
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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);}
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Scala 59 chars:

def f(i:String)=(0L/:i.split("\\.").map(_.toInt))(_*256+_)
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Perl with builtins (35):

unpack"N",pack"C4",split/\./,shift;  

Perl without builtins (42):

split/\./,shift;$s+=$_<<@_*8while$_=shift;
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Python (No eval) - 67

c=lambda x:long(''.join(["%02X"%long(i) for i in x.split('.')]),16)
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ can you not shorten it a few more characters by using int() instead of long()? \$\endgroup\$ – Wug Sep 24 '12 at 6:58
0
\$\begingroup\$

Mathematica, 53 chars

f=FromDigits;f[f/@StringSplit[InputString[],"."],256]
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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 
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Burlesque - 11 bytes

'.;;ri256ug

'.;;           split by `.`
    ri         read integer
      256ug    undigits base 256

Try it online.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Kotlin, 50 bytes

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

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.