0

I have string that i received through Ethernet port and i have decoded it like this:

data, address = p1.recvfrom(1040)  
text = data.decode('ascii')
stri = ''  
for i in text:
    stri = + ord(i)  

Is there a way that doesn't require for loop, that can give me same string right away?

plukic
  • 47
  • 1
  • 9

2 Answers2

1

You could use a one-liner if you just want to minimize your code :

stri = ''.join(str(ord(c)) for c in text)

Or use the map function if you really don't want to use a loop :

stri = ''.join(map(lambda c: str(ord(c)),text))
Aurel
  • 631
  • 3
  • 18
0

It can be made simple with map and ord here is the example

>>> reduce(lambda x, y: str(x)+str(y), map(ord,"hello world"))
'10410110810811132119111114108100'

Source: Convert string to ASCII value python

Community
  • 1
  • 1
Mani
  • 933
  • 6
  • 15