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
4 Answers4
You can achieve this usingint() withbase argument.
You can know more about
int(x,base)-here
- Split the string at
'.'and store it in a listlst - For every item in
lst, convert the item (binary string) to decimal usingint(item, base=2)and then convert it into string type. - Join the contents of
lstusing.
s = '00000000.00000000.00001111.11111111'lst = s.split('.')lst = [str(int(i,2)) for i in lst]print('.'.join(lst))# Output0.0.15.255Comments
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'Comments
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.255Comments
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('.')))Comments
Explore related questions
See similar questions with these tags.


