30
\$\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\$
9
  • 7
    \$\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, 2020 at 12:49
  • 2
    \$\begingroup\$ Is it allowed to use a string as input instead of a list of binary digits? \$\endgroup\$ Jan 10, 2020 at 13:31
  • \$\begingroup\$ I have added a brief description of how the qaud-dotted notation is formed @LuisMendo. \$\endgroup\$
    – mabel
    Jan 10, 2020 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, 2020 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, 2020 at 4:00

45 Answers 45

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)
\$\endgroup\$
2
  • \$\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, 2020 at 11:05
  • \$\begingroup\$ Using 52 bytes: x->join(parse.(Int,x[n:n+7] for n=1:8:25;base=2),:.) \$\endgroup\$ Feb 7, 2020 at 12:35
1
\$\begingroup\$

Vyxal, 45 bitsv2, 5.625 bytes

8ẇvB\.j

Try it Online!

7 bytes -> 5.625 bytes.

Explained

8ẇvB\.j­⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁣‏⁠‎⁡⁠⁤‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏‏​⁡⁠⁡‌­
8ẇ       # ‎⁡Split into chunks of length 8
  vB     # ‎⁢Convert each to binary. Pretty sure it should work without the v. Could be a bug
    \.j  # ‎⁣Join on "."s
💎

Created with the help of Luminespire.

\$\endgroup\$
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.

\$\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
\$\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.

\$\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!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can remove the &255 by using %hhu. \$\endgroup\$
    – S.S. Anne
    Jan 11, 2020 at 0:43
  • \$\begingroup\$ I think the final newline is not required. To leave it out saves 2 bytes. \$\endgroup\$ Jan 13, 2020 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.

\$\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

\$\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.

\$\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"))
\$\endgroup\$
1
  • 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, 2020 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
\$\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!

\$\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!

\$\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)
    )
)
\$\endgroup\$
0
\$\begingroup\$

Thunno 2, 6 bytes

8ẇḂ'.j

Try it online!

4ẆḂ'.j

Try it online!

Explanation

         # Implicit input
 ẇ       # Split the input into
8        # chunks of length eight
 Ẇ       # Or, split the input into
4        # four equal-sized chunks
  Ḃ      # Then, convert each from binary
   '.j  '# And join the list by "."s
         # Implicit output
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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