Change in pysim[master]: start using python3 bytearray for our b2h/h2b types

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

laforge gerrit-no-reply at lists.osmocom.org
Tue Mar 2 06:36:08 UTC 2021


laforge has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/23174 )


Change subject: start using python3 bytearray for our b2h/h2b types
......................................................................

start using python3 bytearray for our b2h/h2b types

The code was written long ago, when the python3 bytearray type
probably didn't exist yet, or was at least not known.  Let's stop
using string types with binary bytes inside, and instead standardize
on two types:
 * bytearray for binary data
 * string for hexadecimal nibbles representing that binary data

Change-Id: I8aca84b6280f9702b0e2aba2c9759b4f312ab6a9
---
M pySim/utils.py
1 file changed, 8 insertions(+), 6 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/74/23174/1

diff --git a/pySim/utils.py b/pySim/utils.py
index 915562f..52d2259 100644
--- a/pySim/utils.py
+++ b/pySim/utils.py
@@ -22,10 +22,12 @@
 
 
 def h2b(s):
-	return ''.join([chr((int(x,16)<<4)+int(y,16)) for x,y in zip(s[0::2], s[1::2])])
+    """convert from a string of hex nibbles to a sequence of bytes"""
+    return bytearray.fromhex(s)
 
-def b2h(s):
-	return ''.join(['%02x'%ord(x) for x in s])
+def b2h(b):
+    """convert from a sequence of bytes to a string of hex nibbles"""
+    return b.hex()
 
 def h2i(s):
 	return [(int(x,16)<<4)+int(y,16) for x,y in zip(s[0::2], s[1::2])]
@@ -334,7 +336,7 @@
 	msisdn_lhv = ef_msisdn[xlen:]
 
 	# Parse the length (in bytes) of the BCD encoded number
-	bcd_len = ord(msisdn_lhv[0])
+	bcd_len = msisdn_lhv[0]
 	# BCD length = length of dial num (max. 10 bytes) + 1 byte ToN and NPI
 	if bcd_len == 0xff:
 		return None
@@ -342,8 +344,8 @@
 		raise ValueError("Length of MSISDN (%d bytes) is out of range" % bcd_len)
 
 	# Parse ToN / NPI
-	ton = (ord(msisdn_lhv[1]) >> 4) & 0x07
-	npi = ord(msisdn_lhv[1]) & 0x0f
+	ton = (msisdn_lhv[1] >> 4) & 0x07
+	npi = msisdn_lhv[1] & 0x0f
 	bcd_len -= 1
 
 	# No MSISDN?

-- 
To view, visit https://gerrit.osmocom.org/c/pysim/+/23174
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I8aca84b6280f9702b0e2aba2c9759b4f312ab6a9
Gerrit-Change-Number: 23174
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210302/36364762/attachment.htm>


More information about the gerrit-log mailing list