Convert a 32 bit binary IPv4 address to its quad-dotted notation
x86-16 machine code, IBM PC DOS, 54 47 45 bytes
Binary:
00000000: be82 00b3 04b1 08ac d0d8 d0d4 e2f9 8ac4 ................
00000010: 41d4 0a50 8ac4 84c0 75f6 580c 30b4 0ecd A..P....u.X.0...
00000020: 10e2 f74b 7406 b02e cd10 ebd9 c3 ...Kt........
Build and test BIN2IP.COM
using xxd -r
from above.
Unassembled listing:
BE 0082 MOV SI, 82H ; command line input address
B3 04 MOV BL, 4 ; loop 4 bytes
BYTE_LOOP:
B1 08 MOV CL, 8 ; loop 8 bits
BIT_LOOP:
AC LODSB ; load next bit char into AL
D0 D8 RCR AL, 1 ; put LSB of char into CF
D0 D4 RCL AH, 1 ; put CF into LSB of byte value, then shift left
E2 F9 LOOP BIT_LOOP ; continue bit loop
8A C4 MOV AL, AH ; put byte result into AL
GET_DIGIT:
D4 0A AAM ; byte divide by 10, AH = AL / 10, AL = AL % 10
50 PUSH AX ; save remainder in AL on stack
8A C4 MOV AL, AH ; put quotient back into AL
41 INC CX ; increment decimal digit count
D4 C0 TEST AL, AL ; quotient = 0?
75 F6 JNZ GET_DIGIT ; if not, continue looping
PRINT_DIGIT:
58 POP AX ; restore digit in AL
0C 30 OR AL, '0' ; ASCII convert
B4 0E MOV AH, 0EH ; BIOS write char function
CD 10 INT 10H ; write to console
E2 F7 LOOP PRINT_DIGIT ; loop until done
4B DEC BX ; is last byte?
74 06 JZ END_LOOP ; if so, don't display a '.'
B0 2E MOV AL, '.' ; otherwise display '.'
CD 10 INT 10H ; write to console
END_LOOP:
75 D7 JNZ BYTE_LOOP ; continue byte loop
C3 RET ; exit to DOS
Output:
A standalone PC DOS executable. Input is command line, output to console.
Notes:
The "interesting part" (converting binary string to bytes) is about 15 bytes, whereas the rest of code is writing the itoa()
function to convert binary bytes into a decimal string representation for display.
- -2 bytes eliminating unnecessary
PUSH/POP
thx to @PeterCordes!
05AB1E, 6 bytes
4äC'.ý
Try it online or verify all test cases.
Explanation:
4ä # Convert the (implicit) input-string into 4 equal-sized parts
C # Convert each part from binary to an integer
'.ý '# Join this list by "."
# (after which the result is output implicitly)
C (gcc), 48 bytes
i;f(n){for(i=4;i--;)printf(".%hhu"+i/3,n>>i*8);}
Takes as input a 32-bit integer.
Thanks to ceilingcat and gastropner for getting this answer where it is now!
Try it online!