Skip to content

Commit

Permalink
Python3: use Python3 versions
Browse files Browse the repository at this point in the history
The code is also working on Python2.

Thanks to Norman DENAYER for the patch
  • Loading branch information
LudovicRousseau authored and moreati committed Jun 30, 2015
1 parent 0aaca40 commit 1fd9457
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions smartcard/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def toASCIIBytes(stringtoconvert):
[78, 117, 109, 98, 101, 114, 32, 49, 48, 49]
"""

return map(ord, list(stringtoconvert))
return list(map(ord, list(stringtoconvert)))


def toASCIIString(bytelist):
Expand Down Expand Up @@ -97,14 +97,10 @@ def toBytes(bytestring):
>>> toBytes("3B6500 009C1101 0103")
[59, 101, 0, 0, 156, 17, 1, 1, 3]
"""
import struct
import re
packedstring = ''.join(re.split('\W+', bytestring))
packedstring = bytestring.replace(' ', '')
try:
return reduce(lambda x, y: x + [int(y, 16)],
struct.unpack('2s' * (len(packedstring) / 2),
packedstring), [])
except (struct.error, ValueError):
return list(map(lambda x: int(''.join(x), 16), zip(*[iter(packedstring)] * 2)))
except KeyError:
raise TypeError('not a string representing a list of bytes')


Expand Down Expand Up @@ -231,10 +227,7 @@ def HexListToBinString(hexlist):
>>> HexListToBinString([78, 117, 109, 98, 101, 114, 32, 49, 48, 49])
'Number 101'
"""
binstring = ""
for byte in hexlist:
binstring = binstring + chr(eval('0x%x' % byte))
return binstring
return ''.join(map(chr, hexlist))


# FIXME This appears to duplicate to ASCIIBytes()
Expand All @@ -243,10 +236,7 @@ def BinStringToHexList(binstring):
>>> BinStringToHexList("Number 101")
[78, 117, 109, 98, 101, 114, 32, 49, 48, 49]
"""
hexlist = []
for byte in binstring:
hexlist = hexlist + [ord(byte)]
return hexlist
return list(map(ord, binstring))


def hl2bs(hexlist):
Expand Down

0 comments on commit 1fd9457

Please sign in to comment.