title: Convert binary file to bytearray in Python 3
body: <p>Currently I am trying to use <a href="https://github.com/peplin/pygatt" rel="nofollow noreferrer">pygatt</a> to send data to a ble characteristic but it use a bytearray as argument.
  My input is a binary file, eg:</p>
  <pre><code>$ xxd file.bin 
  00000000: 0300 1100 0022 0100 0021 8ff6 82ce 8dad  .....&quot;...!......
  00000010: 54                                       T
  </code></pre>
  <p>What I want is:</p>
  <pre><code>&gt;&gt;&gt; bytearray([0x03, 0x00, 0x11, 0x00, 0x00, 0x22, 0x01, 0x00, 0x00, 0x21, 0x8f, 0xf6, 0x82, 0xce, 0x8d, 0xad, 0x54])
  </code></pre>
  <p>So maybe my comprehension of this low level approach is wrong but here what I do:</p>
  <pre><code>import binascii
  import hexdump
  
  my_file = &quot;file.bin&quot;
  with open(my_file, 'rb') as file_t:
  
      # read the file as xxd do
      blob_data = hexdump.hexdump(file_t, result='return')
      print(blob_data)
  
  
  with open(my_file, 'rb') as file_t:
      blob_data = binascii.hexlify(file_t.read())
      print(blob_data)
  
  with open(my_file, 'rb') as file_t:
      blob_data = bytearray(file_t.read())
      print(blob_data)
  </code></pre>
  <p>The output is:</p>
  <pre><code>00000000: 03 00 11 00 00 22 01 00  00 21 8F F6 82 CE 8D AD  .....&quot;...!......
  00000010: 54                                                T
  b'030011000022010000218ff682ce8dad54'
  bytearray(b'\x03\x00\x11\x00\x00&quot;\x01\x00\x00!\x8f\xf6\x82\xce\x8d\xadT')
  </code></pre>
  <p>But starting this point I am pretty lost on how to convert it to bytearray, the last implementation is not far away but the some characters are converted to their ASCII equivalent (eg \x22 =&gt; &quot;)</p>
  <p>Anyone have an idea on how to do that ?
  Thanks guys</p>