<p>laforge has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.osmocom.org/c/pysim/+/23174">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">start using python3 bytearray for our b2h/h2b types<br><br>The code was written long ago, when the python3 bytearray type<br>probably didn't exist yet, or was at least not known.  Let's stop<br>using string types with binary bytes inside, and instead standardize<br>on two types:<br> * bytearray for binary data<br> * string for hexadecimal nibbles representing that binary data<br><br>Change-Id: I8aca84b6280f9702b0e2aba2c9759b4f312ab6a9<br>---<br>M pySim/utils.py<br>1 file changed, 8 insertions(+), 6 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/74/23174/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/pySim/utils.py b/pySim/utils.py</span><br><span>index 915562f..52d2259 100644</span><br><span>--- a/pySim/utils.py</span><br><span>+++ b/pySim/utils.py</span><br><span>@@ -22,10 +22,12 @@</span><br><span> </span><br><span> </span><br><span> def h2b(s):</span><br><span style="color: hsl(0, 100%, 40%);">-     return ''.join([chr((int(x,16)<<4)+int(y,16)) for x,y in zip(s[0::2], s[1::2])])</span><br><span style="color: hsl(120, 100%, 40%);">+    """convert from a string of hex nibbles to a sequence of bytes"""</span><br><span style="color: hsl(120, 100%, 40%);">+    return bytearray.fromhex(s)</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-def b2h(s):</span><br><span style="color: hsl(0, 100%, 40%);">-     return ''.join(['%02x'%ord(x) for x in s])</span><br><span style="color: hsl(120, 100%, 40%);">+def b2h(b):</span><br><span style="color: hsl(120, 100%, 40%);">+    """convert from a sequence of bytes to a string of hex nibbles"""</span><br><span style="color: hsl(120, 100%, 40%);">+    return b.hex()</span><br><span> </span><br><span> def h2i(s):</span><br><span>  return [(int(x,16)<<4)+int(y,16) for x,y in zip(s[0::2], s[1::2])]</span><br><span>@@ -334,7 +336,7 @@</span><br><span>       msisdn_lhv = ef_msisdn[xlen:]</span><br><span> </span><br><span>    # Parse the length (in bytes) of the BCD encoded number</span><br><span style="color: hsl(0, 100%, 40%);">- bcd_len = ord(msisdn_lhv[0])</span><br><span style="color: hsl(120, 100%, 40%);">+  bcd_len = msisdn_lhv[0]</span><br><span>      # BCD length = length of dial num (max. 10 bytes) + 1 byte ToN and NPI</span><br><span>       if bcd_len == 0xff:</span><br><span>          return None</span><br><span>@@ -342,8 +344,8 @@</span><br><span>            raise ValueError("Length of MSISDN (%d bytes) is out of range" % bcd_len)</span><br><span> </span><br><span>      # Parse ToN / NPI</span><br><span style="color: hsl(0, 100%, 40%);">-       ton = (ord(msisdn_lhv[1]) >> 4) & 0x07</span><br><span style="color: hsl(0, 100%, 40%);">-        npi = ord(msisdn_lhv[1]) & 0x0f</span><br><span style="color: hsl(120, 100%, 40%);">+   ton = (msisdn_lhv[1] >> 4) & 0x07</span><br><span style="color: hsl(120, 100%, 40%);">+   npi = msisdn_lhv[1] & 0x0f</span><br><span>       bcd_len -= 1</span><br><span> </span><br><span>     # No MSISDN?</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/c/pysim/+/23174">change 23174</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/c/pysim/+/23174"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: pysim </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I8aca84b6280f9702b0e2aba2c9759b4f312ab6a9 </div>
<div style="display:none"> Gerrit-Change-Number: 23174 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: laforge <laforge@osmocom.org> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>