1

I want to make an int from a string in python. I thought to translate the string first to binary like this:

st = 'string'binSt = ' '.join(format(ord(x), '08b') for x in st)

returns this:

01110011 01110100 01110010 01101001 01101110 01100111

And then I want to translate the binary( in groups of 8) to integers which should to return this:

115 116 114 105 110 103

How can I do this?Is there maybe a special function in python or something?

Martijn Pieters's user avatar
Martijn Pieters
1.1m326 gold badges4.2k silver badges3.4k bronze badges
askedApr 26, 2016 at 16:18
svs's user avatar
2
  • 1
    This is probably what you need:stackoverflow.com/a/8928256/5712767CommentedApr 26, 2016 at 16:22
  • indeed, sorry, I've edited itCommentedApr 26, 2016 at 16:37

5 Answers5

2

You can use theint() function:

result = ''st = 'string'binSt = ' '.join(format(ord(x), '08b') for x in st)binSt_split = binSt.split()for split in binSt_split:    result = result + str(int(split,2) + ' 'print result
answeredApr 26, 2016 at 16:26
Scratch'N'Purr's user avatar
Sign up to request clarification or add additional context in comments.

Comments

2

You can simply do

r = [int(numbs, 2) for numbs in binSt.split(' ')]

int(str, baseNumber) will read the stringstr and convert it toint usingbaseNumber

soint("0xFF", 16) = 255 andint("11", 2) = 3

answeredApr 26, 2016 at 16:30
Nicolas's user avatar

Comments

2

Why not use abytearray?

>>> barr = bytearray('string')>>> barr[0]115

Bytearray does exactly what you want -- It interprets each character in the string as an integer in the range from0 ->255.

answeredApr 26, 2016 at 16:30
mgilson's user avatar

Comments

1

Using the solution for binary to int here:Convert base-2 binary number string to int

 binSt = ' '.join([str(int(format(ord(x), '08b'), 2)) for x in st])

And if you just want an array of ints

 int_array = [int(format(ord(x), '08b'), 2) for x in st]

Addressing SpoonMeiser comments. you can avoid intermediate conversions with ord(x)

 int_array = [ord(x) for x in st]
answeredApr 26, 2016 at 16:26
WreckeR's user avatar

3 Comments

Having the int -> binary and binary -> int on one line like this seems pointless. If you didn't want the intermediate value (the binary representation) you'd just useord(x)
I was answering the OPs question to translate the binary strings to ints. I agree the extra conversions can be avoided.
I just mean that it probably a less helpful answer written this way. I think it's implied that the OP actually wanted the binary representation for some reason
1

String to binary and then to decimal :

st = 'string'binSt = ' '.join(format(ord(x), '08b') for x in st)binSt##'01110011 01110100 01110010 01101001 01101110 01100111'bin=binSt.split()bin##['01110011', '01110100', '01110010', '01101001', '01101110', '01100111']print(map(lambda x: int(x,2), bin))##[115, 116, 114, 105, 110, 103]

## is used for outputs

answeredApr 26, 2016 at 16:26
Ani Menon'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.