title: Pack char to bytes in python using struct
body: <p>I am trying to pack a char to bytes with python using the struct package but my code won't return 4 bytes when packing the char using this code:</p>
  
  <pre><code>def charToHex(s):
  #check if string is unicode
  if isinstance(s, str):
      print(struct.pack('&lt;c', 'a'.encode(encoding='utf-8')))
      return '{:02x}'.format(struct.unpack('&lt;I', struct.pack('&lt;c', s.encode('utf-8')))[0])
  
  #check if input is already a byte
  elif isinstance(s, bytes):
      return '{:02x}'.format(struct.unpack('&lt;I', struct.pack('&lt;c', s))[0])
  
  else:
      raise Exception()
  </code></pre>
  
  <p>Can anyone explain to me why this won't work? I am just trying to convert the unicode char to 4 bytes and unpack it but it won't even pack correct.</p>