2

I am working on an IPV4 breakdown where I have the necessary values in a string variable to represent the binary

(example:00000000.00000000.00001111.11111111) This is a string

I need a way to turn this string into binary to then properly convert it to it's proper integer value

(in this case0.0.15.255)

I've seen posts asking about something similar but attempting to apply it to what I'm working on has been unsuccessful

Apologies if this made no sense this is my first time posing a question here

ThePyGuy's user avatar
ThePyGuy
18.5k5 gold badges24 silver badges55 bronze badges
askedJun 29, 2021 at 7:16
Matteo's user avatar

4 Answers4

1

You can achieve this usingint() withbase argument.

You can know more aboutint(x,base) -here

  • Split the string at'.' and store it in a listlst
  • For every item inlst, convert the item (binary string) to decimal usingint(item, base=2) and then convert it into string type.
  • Join the contents oflst using.
s = '00000000.00000000.00001111.11111111'lst = s.split('.')lst = [str(int(i,2)) for i in lst]print('.'.join(lst))
# Output0.0.15.255
answeredJun 29, 2021 at 7:24
Ram's user avatar
Sign up to request clarification or add additional context in comments.

Comments

0

First split the string on. then convert each to integer equivalent of the binary representation usingint builtin passingbase=2, then convert to string, finally join them all on.

>>> text = '00000000.00000000.00001111.11111111'>>> '.'.join(str(int(i, base=2)) for i in text.split('.'))# output'0.0.15.255'
answeredJun 29, 2021 at 7:21
ThePyGuy's user avatar

Comments

0

You should split the data, convert and combine.

data = "00000000.00000000.00001111.11111111"data_int = ".".join([str(int(i, 2)) for i in data.split(".")])print(data_int)  # 0.0.15.255
answeredJun 29, 2021 at 7:22
Prudhvi's user avatar

Comments

0

Welcome! Once you have a string like this

s = '00000000.00000000.00101111.11111111'

you may get your integers in one single line:

int_list = list(map(lambda n_str: int(n_str, 2), s.split('.')))
answeredJun 29, 2021 at 7:37
Francesco Pinna's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.