FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithms-python/algorithms/maths/decimal_to_binary_ip.py at master · Mu-L/algorithms-python · GitHub
Mu-L
/
algorithms-python
Public
forked from
keon/algorithms
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithms-python
/
algorithms
/
maths
/
decimal_to_binary_ip.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
35 lines (32 loc) · 914 Bytes
Breadcrumbs
algorithms-python
/
algorithms
/
maths
/
decimal_to_binary_ip.py
Copy path
File metadata and controls
35 lines (32 loc) · 914 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""
Given an ip address in dotted-decimal representation, determine the
binary representation. For example,
decimal_to_binary(255.0.0.5) returns 11111111.00000000.00000000.00000101
accepts string
returns string
"""
def
decimal_to_binary_util
(
val
):
"""
Convert 8-bit decimal number to binary representation
:type val: str
:rtype: str
"""
bits
=
[
128
,
64
,
32
,
16
,
8
,
4
,
2
,
1
]
val
=
int
(
val
)
binary_rep
=
''
for
bit
in
bits
:
if
val
>=
bit
:
binary_rep
+=
str
(
1
)
val
-=
bit
else
:
binary_rep
+=
str
(
0
)
return
binary_rep
def
decimal_to_binary_ip
(
ip
):
"""
Convert dotted-decimal ip address to binary representation with help of decimal_to_binary_util
"""
values
=
ip
.
split
(
'.'
)
binary_list
=
[]
for
val
in
values
:
binary_list
.
append
(
decimal_to_binary_util
(
val
))
return
'.'
.
join
(
binary_list
)
Back
|
FazBrowse Home
|
New Git URL