Skip to content

Commit

Permalink
toASCIIString(): replace non-ASCII char by '.'
Browse files Browse the repository at this point in the history
Bytes with value lower than 32 (' ') or higher than 127 (DEL) are
replaced by '.' to get only printable characters.
  • Loading branch information
LudovicRousseau committed Apr 14, 2020
1 parent f4a8ae4 commit 203487d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
11 changes: 10 additions & 1 deletion smartcard/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,18 @@ def toASCIIString(bytelist):
>>> toASCIIString([0x4E,0x75,0x6D,0x62,0x65,0x72,0x20,0x31,0x30,0x31])
'Number 101'
>>> toASCIIString([0x01, 0x20, 0x80, 0x7E, 0xF0])
". .~."
"""

return ''.join(map(chr, bytelist))
res = list()
for b in bytelist:
if b < 32 or b > 127:
c = '.'
else:
c = chr(b)
res.append(c)
return ''.join(res)


def toBytes(bytestring):
Expand Down
4 changes: 4 additions & 0 deletions test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def test_toASCIIString(self):
data_out = "Number 101"
self.assertEqual(toASCIIString(data_in), data_out)

data_in = [0x01, 0x20, 0x80, 0x7E, 0xF0]
data_out = ". .~."
self.assertEqual(toASCIIString(data_in), data_out)

def test_toGSM3_38Bytes(self):
data_in = "@Pascal"
data_out = [0x00, 0x50, 0x61, 0x73, 0x63, 0x61, 0x6C]
Expand Down

0 comments on commit 203487d

Please sign in to comment.