28
\$\begingroup\$

Challenge

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

Quad-dotted decimal

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

\$\endgroup\$
  • 6
    \$\begingroup\$ Input will always be a 32 bit binary number but your examples are binary lists. Is that also an acceptable input form? \$\endgroup\$ – Adám Jan 10 at 12:49
  • 2
    \$\begingroup\$ Is it allowed to use a string as input instead of a list of binary digits? \$\endgroup\$ – Galen Ivanov Jan 10 at 13:31
  • \$\begingroup\$ I have added a brief description of how the qaud-dotted notation is formed @LuisMendo. \$\endgroup\$ – mabel Jan 10 at 13:31
  • \$\begingroup\$ @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. \$\endgroup\$ – mabel Jan 10 at 13:33
  • 2
    \$\begingroup\$ I'm not clear, can we take in an actual number as input, like 2130706433 to give 127.0.0.1? \$\endgroup\$ – xnor Jan 11 at 4:00

43 Answers 43

1
2
1
\$\begingroup\$

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.

Reductions per comments below:

  • 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)
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ x->join(parse.(Int,(x[n:n+7] for n=1:8:25),base=2),".") for 55 bytes - from a suggested edit. \$\endgroup\$ – Lyxal Feb 7 at 11:05
  • \$\begingroup\$ Using 52 bytes: x->join(parse.(Int,x[n:n+7] for n=1:8:25;base=2),:.) \$\endgroup\$ – user3263164 Feb 7 at 12:35
0
\$\begingroup\$

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.

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

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

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.

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

[C (gcc clang)], 97 85 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!

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

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.

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

SmileBASIC, 49 bytes

INPUT X
RGBREAD X OUT A,B,C,D?A;".";B;".";C;".";D

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

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

Haskell, 52 bytes

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.

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

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"))
|improve this answer|||||
\$\endgroup\$
  • 2
    \$\begingroup\$ 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()) \$\endgroup\$ – Jo King Jan 14 at 0:47
0
\$\begingroup\$

Perl, 33 bytes

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

Try it online!

Read in right-to-left order:

  1. Encode input using pack
  2. Extract four unsigned bytes with unpack
  3. Intercalate . separators with join
|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

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!

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

Fortran (GFortran), 58 bytes

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

Try it online!

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

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
            io.read(),                       --    in string
            "%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)
    )
)
|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.