# Challenge

Write a program or function that converts a 32 bit binary number to its quad-dotted decimal notation (often used for representing IPv4)

A quad-dotted decimal is formed like so:

1. Split the binary representation into its 4 individual bytes
2. Convert each byte into denary
3. place "." between each number

Example:

input:  10001011111100010111110001111110
step 1: 10001011 11110001 01111100 01111110
step 2:    139      241      124      126
step 3: 139.241.124.126


# I/O examples

input --> output

10001011111100010111110001111110 --> 139.241.124.126
00000000000000000000000000000000 --> 0.0.0.0
01111111000000000000000000000001 --> 127.0.0.1
11000000101010000000000111111111 --> 192.168.1.255


# Rules

• Input will always be a 32 bit binary number but your examples are binary lists. Is that also an acceptable input form? – Adám Jan 10 at 12:49
• Is it allowed to use a string as input instead of a list of binary digits? – Galen Ivanov Jan 10 at 13:31
• I have added a brief description of how the qaud-dotted notation is formed @LuisMendo. – mabel Jan 10 at 13:31
• @Adám this is actually how I meant for the challenge to be interpreted, but I guess I couldn't find the right word. I have added it as an acceptable input form, but left the 32 bit binary number as valid input also. – mabel Jan 10 at 13:33
• I'm not clear, can we take in an actual number as input, like 2130706433 to give 127.0.0.1? – xnor Jan 11 at 4:00

# Julia, 58 52 bytes

x->join(parse.(Int,x[n:n+7] for n=1:8:25;base=2),:.)

Takes the input string and uses a comprehension to create an array of the 8 bits. Then parses each element (via 'broadcasting' with the . into an Int. Last step is to join with the period.

• 4 byte reduction by simplifying the comprehension indexing
• 1 byte reduction in changing the separator from a string to a symbol (which join converts into a string upon joining with the integers)
• x->join(parse.(Int,(x[n:n+7] for n=1:8:25),base=2),".") for 55 bytes - from a suggested edit. – Lyxal Feb 7 at 11:05
• Using 52 bytes: x->join(parse.(Int,x[n:n+7] for n=1:8:25;base=2),:.) – Simeon Schaub Feb 7 at 12:35

## Batch, 56 bytes

@for /f "tokens=2" %%f in ('ping %1')do @echo %%f&exit/b


Takes input as the first command-line argument. Somewhat slow, but -n1 -w1 can be added to the ping command to speed it up a bit.

# GolfScript, 20 bytes

8/{1/{~}%2base}%'.'*


Try it online!

## Explanation

8/                   # Split into size 8 chunks
{           }%     # For-each over the chunks
1/                # Split into size-1 chunks
{~}%            # Convert each from string to number
2base       # Convert to base 10
'.'* # Join with dots


# Alchemist, 97 bytes

_->3a+7b+i
i+n->i+m
i+0n->j+In_n
j+m->j+2n
j+0m+b->i
j+0m+0b->k+Out_n
k+n->k
k+0n+a->7b+i+Out_"."


Try it online!

a and b count down the bytes and bits remaining, respectively. i, j, and k are phase/step atoms. The current byte is stored in n, with temporary storage in m.

# [C (gcc clang)], 9785 77 bytes

x;f(*s){x=strtoul(s,s,2);printf("%hhu.%hhu.%hhu.%hhu\n",x>>24,x>>16,x>>8,x);}


Try it online!

This version takes a string as input. 8 bytes shaved off thanks to JL2210!

• You can remove the &255 by using %hhu. – S.S. Anne Jan 11 at 0:43
• I think the final newline is not required. To leave it out saves 2 bytes. – David Foerster Jan 13 at 1:17

# Pyth, 10 9 bytes

j\.iR2cz8


Try it online!

Chops input into pieces of length 8 (cz8), then maps int(x, 2) over the result (iR2). Then just joins those with a dot separator (j\.).

-1 by using a more specialized map (R) instead of the generic m.

# SmileBASIC, 49 bytes

INPUT X


Takes a decimal integer as input

the RGB and RGBREAD functions were designed to convert between 32 bit ARGB color values and separate 8 bit channels

s n|(y,x)<-divMod n 256=concat[s y++"."|y>0]++show x


Try it online!

Takes input as number. Repeatedly divMods by 256 until the div is zero.

# Python 3, 44 bytes

Another one using to_bytes trick, but this one also uses f-strings.

'.'.join(f"{b}"for b in n.to_bytes(4,"big"))

• You don't appear to be doing any input or output. To be valid, you should either make this a function by prepending lambda n: to the front, or mark this as REPL and change n to int(input()) – Jo King Jan 14 at 0:47

# Perl, 33 bytes

$_=join'.',unpack'C4',pack'B*',$_


Try it online!

1. Encode input using pack
2. Extract four unsigned bytes with unpack
3. Intercalate . separators with join

# Ruby-p, 34 bytes

Replaces each 8-character subsequence with its decimal conversion plus a dot, then removes the last dot.

gsub(/.{8}/){"#{\$&.to_i 2}."}
chop


Try it online!

# Fortran (GFortran), 58 bytes

integer i(4)                 !Declare 4 int array
print('(3(i0,"."),i0)'),i    !Print as 3 auto-length ints followed by a '.' and then the last int
end


Try it online!

# Lua, 82 bytes

print(io.read():gsub(("%d"):rep(8),function(n)return'.'..tonumber(n,2)end):sub(2))


Try it online!

Takes input from stdin and writes to stdout. Works on Lua 5.0+.

This works by using string.gsub's ability to replace matches using a helper function. The function used here replaces a string of eight binary digits with a . followed by the digits in decimal. The :sub(2) at the end removes the extraneous . at the beginning of the resulting string.

Un-golfed version + extra explanation:

print(                                       -- print
string.sub(                              --  substring
string.gsub(                         --   replace
"%d%d%d%d%d%d%d%d",              --    replace
function(n)                      --    with
return                       --     result of
'.' ..                   --      concatenate with
tonumber(                --      string to number
n,                   --       convert this
2                    --       with base
)
end
),
2                                    --   starting at character (1-index)
)
)