Вопрос

In Python 2.7 I am trying to a covert a String, eg '1001' to the binary number '1001' so that I can perform binary arithmetic on it.

Specifically I want to add bin(1) and left shift the binary string.

Is there an easy way to do this (preferably in the python library)?

Это было полезно?

Решение

int takes a base parameter:

>>> int('1001', 2)
9
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top