title: How to convert from string to 16-bit unsigned integer in python?
body: <p>I'm currently working on some encoding and decoding of the string in python. I was supposed to convert some code from C# to python, however I encountered some problem as below:</p>
  
  <p>So now I have a string that looks like this: <code>21-20-89-00-67-00-45-78</code>
  The code was supposed to eliminates the <code>-</code> in between the numbers, and packed 2 integers into 1 group, then convert them into bytes. In C#, it was done like this:</p>
  
  <pre><code>var value = "21-20-89-00-67-00-45-78";
  var valueNoDash = value.Replace("-", null);
  for (var i = 0; i &lt; DataSizeInByte; i++)
  {
      //convert every 2 digits into 1 byte
      Data[i] = Convert.ToByte(valueNoDash.Substring(i * 2, 2), 16);
  }
  </code></pre>
  
  <p>The above code represents Step 1: Remove <code>-</code> from the string, Step 2: using Substring method to divide them into 2 digits in 1 group, Step 3: use Convert.ToByte with base 16 to convert them into 16-bit unsigned integer. The results in <code>Data</code> is </p>
  
  <pre><code>33
  32
  137
  0
  103
  0
  69
  120
  </code></pre>
  
  <p>So far I have no problem with this C# code, however when I try to do the same in python, I could not get to the same result as the C# code. My python code are as below:</p>
  
  <pre><code>from textwrap import wrap
  import struct
  
  values = "21-20-89-00-67-00-45-78"
  values_no_dash = a.replace('-', '')
  values_grouped = wrap(b, 2)
  values_list = []
  
  for value in values_grouped:
      values_list.append(struct.pack('i', int(value)))
  </code></pre>
  
  <p>In python, it gives me list of bytes in hex value, which is as below:</p>
  
  <pre><code>b'\x15\x00\x00\x00'
  b'\x14\x00\x00\x00'
  b'Y\x00\x00\x00'
  b'\x00\x00\x00\x00'
  b'C\x00\x00\x00'
  b'\x00\x00\x00\x00'
  b'-\x00\x00\x00'
  b'N\x00\x00\x00'
  </code></pre>
  
  <p>This is in bytes object, however when I converted this object into Decimal, it gives me the exact same value as the original string: <code>21, 20, 89, 0, 67, 0, 45, 78</code>. </p>
  
  <p>Which means I did not convert successfully into 16-bit unsigned integer right? How can I do this in python? I've tried using <code>str.encode()</code> but the result still different. How can I achieve what C# had done in python?</p>
  
  <p>Thanks and appreciates if anyone can help! </p>