IPv4 Integer Conversion Function

17

2

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

Kyle Rozendo

Posted 2011-02-02T10:00:25.613

Reputation: 291

2I think this would be better if there was a restriction in place prohibiting a language's built-in functions. – Nathan Osman – 2011-02-06T20:04:20.747

@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 Rozendo – 2011-02-07T05:51:43.700

Answers

11

PHP - 21 Characters

<?=ip2long($argv[1]);

ircmaxell

Posted 2011-02-02T10:00:25.613

Reputation: 897

8

MySQL - 20 Characters

SELECT INET_ATON(s);

ircmaxell

Posted 2011-02-02T10:00:25.613

Reputation: 897

8

C: 79 characters

main(i,a)char**a;{i=i<<8|strtol(a[1],a+1,0);*a[1]++?main(i,a):printf("%u",i);}

EDIT: removed C++, would not compile without headers; with GCC, the printf and strtol function calls trigger built-in functions, hence headers can be skipped. Thx to @ugoren for the tips. This will compile as is without additional options to gcc.

EDIT2: return is actually redundant :)

Nim

Posted 2011-02-02T10:00:25.613

Reputation: 269

This won't work in C++, you can't call main recursively. – Scott Logan – 2012-02-26T17:40:07.823

Very nice. Can save a few characters though: K&R style saves 6 with main(i,a)char**a;{..., and &a[1] is a+1. – ugoren – 2012-02-28T14:31:47.343

@Bunnit, it will (may not be standards compliant), however the only difference is that printf and strtol are not built-in functions, so need to include two headers explicitly! I'll remove the c++ from the answer... – Nim – 2012-02-28T14:52:30.563

@ugoren, thx for the tips! – Nim – 2012-02-28T15:16:04.043

@Nim, it is illegal to call main() in C++, it is one of the differences between C and C++. It's explicitly stated in the standard. It might compile but it's UB. – Scott Logan – 2012-02-28T15:47:48.010

Actually the question only asks for a function anyway so it doesn't even have to be main. So you could save an extra six characters by using a single letter name.(Also now it should definitely not compile on any C++ compiler due to the default ints) – Scott Logan – 2012-02-28T16:27:12.220

very clever use of main() :) .. my version was 116bytes. – akira – 2011-02-15T14:21:47.567

I get a segmentation fault. – Nathan Osman – 2011-02-19T03:11:44.747

@George, what is your input and how are you running it? – Nim – 2011-02-21T10:43:44.623

I'm running it with my UserScript through http://codepad.org

– Nathan Osman – 2011-02-21T19:33:55.377

8

Ruby (no builtins/eval) - 47

s=->s{s.split(".").inject(0){|a,b|a<<8|b.to_i}}

Test:

s["192.168.1.1"]
3232235777

Arnaud Le Blanc

Posted 2011-02-02T10:00:25.613

Reputation: 2 286

7

Golfscript -- 16 chars

{[~]2%256base}:f

As a standalone program, this is even shorter at 11.

~]2%256base

Extremely straightforward. Evaluates the input string (~) and puts it into an array []. Since the .s in the string duplicate the top of the stack, we only take every other term in the array (2%). We now have an array which basically represents a base 256 number, so we use a built-in function to do the conversion. (256base).

Nabb

Posted 2011-02-02T10:00:25.613

Reputation: 2 540

Very clever. Now do that for an IPv6 address. :) – Ilmari Karonen – 2012-02-29T17:28:48.617

very clever. i guess base256 is treated differently to say base10 or base16 then where 48=>0? – gnibbler – 2011-02-06T20:05:36.230

@gnibbler: I'm not sure what you're suggesting -- the base function handles all bases the same way, e.g. {:B;{\B*+}*}:base (although the real function is overloaded for conversions the other way). Interesting to note is that base conversion for strings is the same as arrays (as strings are just arrays without nesting, but with a different output format). – Nabb – 2011-02-06T20:27:21.117

yeah i was thinking of base conversions of strings, so i didn't look closely enough at base for my answer – gnibbler – 2011-02-07T00:20:57.690

6

Befunge - 2x11 = 22 characters

So close, Befunge will win one day.

>&+~1+#v_.@
^*4*8*8<

Explanation

The biggest distinguishing feature of Befunge is that instead of being a linear set of instructions like most languages; it is a 2d grid of single character instructions, where control can flow in any direction.

>      v
^      <

These characters change the direction of control when they are hit, this makes the main loop.

 &+~1+

This inputs a number and pushes it onto the stack (&), pops the top two values off the stack, adds them and pushes them back onto the stack (+), inputs a single character and places its ascii value on the stack (~), then pushes 1 onto the stack and adds them (1+).

The interpreter I've been using returns -1 for end of input, some return 0 instead so the 1+ part could be removed for them.

      #v_.@

The # causes the next character to be skipped, then the _ pops a value off the stack and if it is zero sends control right, otherwise sends it left. If the value was zero . pops a value off the stack and outputs it as an integer and @ stops the program. Otherwise v sends control down to the return loop.

^*4*8*8<

This simply multiplies the top value of the stack by 256 and returns control to the start.

Nemo157

Posted 2011-02-02T10:00:25.613

Reputation: 1 891

Pardon my ignorance, but should that be 19 chars? I understand why you say 2x11, but why does it work that way? – Kyle Rozendo – 2011-02-04T19:08:25.373

Befunge is a 2d language, if you look for the >v<^ that is actually the main loop in this program. I guess in this case control doesn't actually pass through those last 3 spaces on the bottom, but I find it easiest to just count Befunge programs as the smallest bounding rectangle; and if you were to try and count control flow you get into trouble with self-modifying programs. – Nemo157 – 2011-02-04T22:37:34.510

5

Ruby (40)

q=->x{x.gsub(/(\d+)\.?/){'%02x'%$1}.hex}

->

q["192.168.1.1"]
=> 3232235777

jsvnm

Posted 2011-02-02T10:00:25.613

Reputation: 441

Nice idea of using regexp. – Hauleth – 2012-02-28T14:56:00.853

Very clever! Also you can write to_i 16 as hex to save some characters. – Paul Prestidge – 2012-03-01T21:54:54.983

thanks @chron that made both this and link 4 characters shorter

– jsvnm – 2012-03-02T08:35:01.690

4

Ruby - 46 chars

require"ipaddr"
def f s;IPAddr.new(s).to_i;end

gnibbler

Posted 2011-02-02T10:00:25.613

Reputation: 14 170

I think that constitutes cheating. ;-) – Chris Jester-Young – 2011-02-02T12:10:16.720

3

Golfscript - 21 chars

{'.'/{~}%{\256*+}*}:f

gnibbler

Posted 2011-02-02T10:00:25.613

Reputation: 14 170

+1 Good solid solution. Don't you wish GolfScript provides bit-shifting operators? ;-) (Though, darned if I know which symbols they should be bound to.) – Chris Jester-Young – 2011-02-02T12:12:18.510

3

Python 56 45

c=lambda x:eval('((('+x.replace('.','<<8)+'))

Alexandru

Posted 2011-02-02T10:00:25.613

Reputation: 5 485

3

C++ - lots of chars

#include <boost/algorithm/string.hpp>
#include <string>
#include <vector>
uint f(std::string p)
{
        std::vector<std::string> x;
        boost::split(x,p,boost::is_any_of("."));
        uint r=0;
        for (uint i = 0; i < x.size(); i++)
                r=r*256+atoi(x[i].c_str());
        return r;
}

grokus

Posted 2011-02-02T10:00:25.613

Reputation: 1 043

@George Edison: using boost helps to get down with the numbers of chars? :) – akira – 2011-02-15T14:24:19.653

3

Bash - 46

Table of content

You will find 4 differently golfed version:

echo $[_=32,`printf "%d<<(_-=8)|" ${1//./ }`0]                        # 46chr
set -- ${1//./ };echo $[$1<<24|$2<<16|$3<<8|$4]                       # 47chr
v=('|%d<<'{24,16,8,0});printf -vv "${v[*]}" ${1//./ };echo $[0$v]     # 65chr
mapfile -td. i<<<$1;for((a=o=0;a<4;o+=i[a]<<(3-a++)*8)){ :;};echo $o  # 68chr

New version! 2018-11-15 More golfed, 46 char

echo $[_=32,`printf "%d<<(_-=8)|" ${1//./ }`0]

Explanation

  • I used $_ for more golfing.
  • Syntax ${1//./ }will substitute every dots . by spaces .
  • so printfwill render something like 192<<(_-=8)|168<<(_-=8)|1<<(_-=8)|1<<(_-=8)|
  • then we will add a 0 after last OR | and
  • preset _ to 32. will read construct from left to right, so $((_-=8)) make 24 at 1st shift, 16 on second, and so on.

in action:

set -- 192.168.1.1
echo $[_=32,`printf "%d<<(_-=8)|" ${1//./ }`0]
3232235777

For fun: trying to get $_ content, after this:

echo $_
3232235777

;-b

set -- 192.168.1.1
echo $_ $[_=32,`printf "%d<<(_-=8)|" ${1//./ }`0] $_
192.168.1.1 3232235777 0

Ok, that's correct 32 - 4 x 8 = 0

In a function:

ip2int() {
    echo $[_=32,`printf "%d<<(_-=8)|" ${1//./ }`0]
}
ip2int 192.168.1.1
3232235777
ip2int 255.255.255.255
4294967295
ip2int 0.0.0.0
0

or into a loop: -> 60

ip2int() {
    for i;do
        echo $[_=32,`printf "%d<<(_-=8)|" ${i//./ }`0]
    done
}

ip2int 192.168.1.1 10.10.104.36 8.8.8.8 1.1.1.1 255.255.255.255 0.0.0.0
3232235777
168454180
134744072
16843009
4294967295
0

bash (v4.1+): 47

First post

set -- ${1//./ };echo $[$1<<24|$2<<16|$3<<8|$4]

Explanation:

  • Syntax ${1//./ }will substitute every dots . by spaces .
  • set -- set positional parameters ($@=($1 $2 $3...))
  • So set -- ${1//./ } will split $1 by dots and set $1, $2, $3 and $4 if string containg 3 dots (and no spaces).

in action:

set -- 192.168.1.1
set -- ${1//./ };echo $[$1<<24|$2<<16|$3<<8|$4]
3232235777

or in a function:

ip2int() {
    set -- ${1//./ }
    echo $[$1<<24|$2<<16|$3<<8|$4]
}
ip2int 192.168.1.1
3232235777
ip2int 0.0.0.0
0

or into a loop: -> 61

for i;do set -- ${i//./ };echo $[$1<<24|$2<<16|$3<<8|$4];done

in action:

ip2int() {
    for i;do
        set -- ${i//./ }
        echo $[$1<<24|$2<<16|$3<<8|$4]
    done
}

ip2int 192.168.1.1 10.10.104.36 8.8.8.8 1.1.1.1 0.0.0.0
3232235777
168454180
134744072
16843009
0

Another version differently golfed: 65

v=('|%d<<'{24,16,8,0});printf -vv "${v[*]}" ${1//./ };echo $[0$v]

Sample:

ip2int() {
    v=('|%d<<'{24,16,8,0});printf -vv "${v[*]}" ${1//./ };echo $[0$v]
}

ip2int 255.255.255.255
4294967295
ip2int 10.10.104.36
168454180

In a loop (+14): 82

ip2int() {
    for i;do
        v=('|%d<<'{24,16,8,0})
        printf -vv "${v[*]}" ${1//./ }
        echo $[0$v]
    done
}

* or a little more ugly: 70*

v=('|%d<<'{24,16,8});printf -vv "${v[*]}" ${1//./ };echo $[0${v%<<2*}]

where printf give some string like |192<<24 |168<<16 |1<<8|1<<24 |0<<16 |0<<8 we have to cut at last <<2....

golfed with mapfile, longer: 68

ip2int() {
    mapfile -td. i<<<$1;for((a=o=0;a<4;o+=i[a]<<(3-a++)*8)){ :;};echo $o
}

or with loop: 82

ip2int() {
    for a;do
      mapfile -td. i<<<$a;for((a=o=0;a<4;o+=i[a]<<(3-a++)*8)){ :;};echo $o
    done
}

F. Hauri

Posted 2011-02-02T10:00:25.613

Reputation: 2 654

could you add a brief explanation? i've been trying to get into bash golfing and i don't follow what's happening with the set -- part. thanks. – Jonah – 2018-11-12T20:27:33.510

@Jonah : set -- foo bar will populate $@ with *foo* as $1 and *bar* as $2. – F. Hauri – 2018-11-13T17:01:01.053

@Jonah Added new version – F. Hauri – 2018-11-13T17:18:31.993

Thanks for the explanation. – Jonah – 2018-11-13T17:23:38.807

New version! more golfed, -1 char!! – F. Hauri – 2018-11-15T10:12:19.880

Fun: Try to look content of $_ after running echo $[_=32,\printf "%d<<(_-=8)|" ${1//./ }`0]` ;-) – F. Hauri – 2018-11-15T10:25:34.763

3

AWK in ~47 chars

First-timer here... Um, not sure how to count this, but without the 'echo' it's 47 chars in AWK. (Not exactly bogey golf, but it's in the hole.)

echo $addr | /bin/awk -F\. '{print $1*16777216+$2*65536+$3*256+$4}'

Full day early for #tbt, too, so I actually met a schedule!!! *8-)

Banner day.

Blair Wyman

Posted 2011-02-02T10:00:25.613

Reputation: 31

1

Welcome! You'd only count the body of what you'd put into an awk script. See this answer. In your case you'd count only {print $1*16777216+$2*65536+$3*256+$4}. Of course you'd have to move the field separator into the program, rather than specifying it as a flag.

– Jonah – 2018-11-13T02:00:21.627

3

PowerShell 66 61

Variation on Joey's answer:

filter I{([ipaddress](($_-split'\.')[3..0]-join'.')).address}

PS C:\> '192.168.1.1' | I
3232235777
PS C:\> '10.10.104.36' | I
168454180
PS C:\> '8.8.8.8' | I
134744072

Ty Auvil

Posted 2011-02-02T10:00:25.613

Reputation: 473

Argh, I must have been stupid to have missed that ... – Joey – 2011-02-15T01:59:45.507

2

Powershell, 47 43 bytes

$args-split'\.'|%{$r=([long]$r-shl8)+$_};$r

Test script:

$f = {

$args-split'\.'|%{$r=([long]$r-shl8)+$_};$r

}

@(
    ,("192.168.1.1",3232235777)
    ,("10.10.104.36",168454180)
    ,("8.8.8.8",134744072)
) | % {
    $s,$expected = $_
    $result = &$f $s
    "$($result-eq$expected): $result"
}

Output:

True: 3232235777
True: 168454180
True: 134744072

mazzy

Posted 2011-02-02T10:00:25.613

Reputation: 4 832

2

hakr14

Posted 2011-02-02T10:00:25.613

Reputation: 1 295

2

Windows PowerShell, 70

Naïve approach:

filter I{[int[]]$x=$_-split'\.'
$x[0]*16MB+$x[1]*64KB+$x[2]*256+$x[3]}

With using System.Net.IPAddress: 76

filter I{([ipaddress]($_-replace('(.+)\.'*3+'(.+)'),'$4.$3.$2.$1')).address}

Test:

> '192.168.1.1'|I
3232235777

Joey

Posted 2011-02-02T10:00:25.613

Reputation: 12 260

2

JavaScript (45 characters)

Requires support for the .reduce() Array method introduced in ES5 and arrow functions.

f=(x)=>x.split('.').reduce((p,c)=>p<<8|c)>>>0

PleaseStand

Posted 2011-02-02T10:00:25.613

Reputation: 5 369

Huh... I didn't know >>> worked like that (converting to unsigned 32 bit integer) – 12Me21 – 2018-11-24T15:30:54.380

2

Befunge-93 - 36 characters

&"~"2+::8****&884**:**&884***&++++.@

MiffTheFox

Posted 2011-02-02T10:00:25.613

Reputation: 631

2

Perl : DIY ( for oneliners. )(40)

$j=3;$i+=($_<<($j--*8))for split/\./,$x;

# Use value in $i

DIY Function(65):

sub atoi{my($i,$j)=(0,3);$i+=($_<<($j--*8))for split'.',shift;$i}

Kent Fredric

Posted 2011-02-02T10:00:25.613

Reputation: 181

With the function version, yes, but the inline version no, because you'd need split q{.} to get around the need to escape shell quotes :/ – Kent Fredric – 2011-02-06T04:59:30.743

You can split by a string, so you save a character by using split'.' rather than split/\./ – anonymous coward – 2011-02-05T10:14:37.200

2

Haskell - 14 chars

(.) a=(256*a+)

usage in GHCi:

Prelude> let (.) a=(256*a+)
Prelude> 192. 168. 0. 1
3232235521

The only problem is that you have to put spaces left or right of the dot, otherwise the numbers will be interpreted as floating point.

quasimodo

Posted 2011-02-02T10:00:25.613

Reputation: 985

mind adding a brief explanation? – Jonah – 2018-11-12T20:28:27.727

the dot infix operator is redefined to do the computation, very smart indeed! – memo – 2018-11-22T12:36:45.683

This is very clever, but it doesn't use the correct input format (because of the spaces) – 12Me21 – 2018-11-24T19:39:54.253

2

C# – 77 chars

Func<string,uint>F=s=>s.Split('.').Aggregate(0u,(c,b)=>(c<<8)+uint.Parse(b));

Mormegil

Posted 2011-02-02T10:00:25.613

Reputation: 1 148

1

Stax, 9 bytes

üL▼ü╛tΓi╨

Run and debug it

recursive

Posted 2011-02-02T10:00:25.613

Reputation: 8 616

1

Perl 6, 16 bytes

{:256[m:g/\d+/]}

Try it online!

nwellnhof

Posted 2011-02-02T10:00:25.613

Reputation: 10 037

1

C (gcc) -m32 / POSIX, 33 bytes

f(a){inet_aton(a,&a);a=ntohl(a);}

Try it online!

On a big-endian platform, you could simply define a macro with -Df=inet_aton for 13 bytes.

nwellnhof

Posted 2011-02-02T10:00:25.613

Reputation: 10 037

1

C# - 120 Characters

float s(string i){var o=i.Split('.').Select(n=>float.Parse(n)).ToList();return 16777216*o[0]+65536*o[1]+256*o[2]+o[3];}

My first code golf - be gentle ;)

Kyle Rozendo

Posted 2011-02-02T10:00:25.613

Reputation: 291

You can remove the spaces around your first '='. However, your main problem is int overflow ;). Remember, an IP address takes up 4 full bytes. – Nellius – 2011-02-02T10:49:35.907

@Nellius - quite right. I didn't even think of checking that, basically checked on compile. Thanks, will fix now. – Kyle Rozendo – 2011-02-02T12:35:32.893

1

D: 84 Characters

uint f(S)(S s)
{
    uint n;
    int i = 4;

    foreach(o; s.split("."))
        n += to!uint(o) << 8 * --i;

    return n;
}

Jonathan M Davis

Posted 2011-02-02T10:00:25.613

Reputation: 705

i don't know D so forgive me if it's whitespace sensitive like python, but this doesn't look golfed. can you remove the double line breaks or the line breaks between semi-colon terminated statements? – Jonah – 2018-11-12T20:25:34.093

1

Python 3.2 (69)

sum((int(j)*4**(4*i)) for i,j in enumerate(input().split('.')[::-1]))

l0nwlf

Posted 2011-02-02T10:00:25.613

Reputation: 61

1

Perl, 14 characters:

sub _{unpack'L>',pop}

# Example usage
print _(10.10.104.36) # prints 168454180

Grimmy

Posted 2011-02-02T10:00:25.613

Reputation: 12 521

5How do you make that 14 characters? I count 21. – Peter Taylor – 2012-10-03T10:49:21.460

1

PHP (no builtins/eval) - 54

<foreach(explode(".",$argv[1])as$b)$a=@$a<<8|$b;echo$a;

Arnaud Le Blanc

Posted 2011-02-02T10:00:25.613

Reputation: 2 286

Shouldn't this open with <?php, not just <? – TRiG – 2012-09-25T16:44:52.750

@TRiG, I believe you can change the PHP opening delimiter in the config file. Useful in this case. – Xeoncross – 2012-09-25T16:56:18.963

@Xeoncross. Ah. Neat. I might try that one day, just to mess with my workmates' heads. – TRiG – 2012-09-25T16:57:46.673

0

Mathematica, 53 chars

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

alephalpha

Posted 2011-02-02T10:00:25.613

Reputation: 23 988

0

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

Jonah

Posted 2011-02-02T10:00:25.613

Reputation: 8 729

0

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.

Comrade SparklePony

Posted 2011-02-02T10:00:25.613

Reputation: 5 784

0

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 

RosLuP

Posted 2011-02-02T10:00:25.613

Reputation: 3 036

0

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.

NotBaal

Posted 2011-02-02T10:00:25.613

Reputation: 131

0

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

Bubbler

Posted 2011-02-02T10:00:25.613

Reputation: 16 616

0

Burlesque - 11 bytes

'.;;ri256ug

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

Try it online.

mroman

Posted 2011-02-02T10:00:25.613

Reputation: 1 382

0

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

Shaggy

Posted 2011-02-02T10:00:25.613

Reputation: 24 623

0

Kotlin, 50 bytes

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

Try it online!

snail_

Posted 2011-02-02T10:00:25.613

Reputation: 1 982

0

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.

Panda0nEarth

Posted 2011-02-02T10:00:25.613

Reputation: 111

0

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.

SpellingD

Posted 2011-02-02T10:00:25.613

Reputation: 121

0

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

Skizz

Posted 2011-02-02T10:00:25.613

Reputation: 2 225

0

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);}

marinus

Posted 2011-02-02T10:00:25.613

Reputation: 30 224

0

Scala 59 chars:

def f(i:String)=(0L/:i.split("\\.").map(_.toInt))(_*256+_)

user unknown

Posted 2011-02-02T10:00:25.613

Reputation: 4 210

0

Perl with builtins (35):

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

Perl without builtins (42):

split/\./,shift;$s+=$_<<@_*8while$_=shift;

ardnew

Posted 2011-02-02T10:00:25.613

Reputation: 2 177

0

Python (No eval) - 67

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

Kevin Brown

Posted 2011-02-02T10:00:25.613

Reputation: 5 756

can you not shorten it a few more characters by using int() instead of long()? – Wug – 2012-09-24T06:58:20.950