title: java's getByte() equivalent in python
body: <p>I am a nebie to python. I have a java method which accepts a string, converts the string to a byte array and returns the byte array. The method looks like this.  </p>
  
  <pre><code>private static byte[] convert(String str) {
          byte[] byteArray = str.getBytes();
          return byteArray;
      }
  </code></pre>
  
  <p><code>convert("sr_shah")</code> results in a byte array like this 
  <code>115 114 95 115 104 97 104</code>. On using <code>Charset.defaultCharset()</code> i came to know that my machine's dfault charectorset is <code>windows-1252</code>.  </p>
  
  <p>Now i need to create exact equivalent of the above method in Python. The problem i am facing now is with  converting string to bytearray. I am unable to get java's getBytes() equivalent in python. I searched in internet and took many help from stackoverflow's previous posts on converting string to byte array but unfortunately none of them worked for me.</p>
  
  <p>The methods i used are <code>bytearray(),bytes(),str.encode()</code>. I used encodings like
   <code>windows-1252,utf_16,utf_8,utf_16_le,utf_16_be,iso-8859-1</code> unfortunately none of them give the right result as i expected(i.e like the byte array i got from java getBytes()) . I am not getting what wrong thing am i doing. this is how i tried in python.</p>
  
  <pre><code>&gt;&gt;&gt; bytearray('sr_shah','windows-1252')
  bytearray(b'sr_shah')
  &gt;&gt;&gt; bytearray('sr_shah','utf_8')
  bytearray(b'sr_shah')
  &gt;&gt;&gt; bytearray('sr_ahah','utf_16')
  bytearray(b'sr_ahah')
  &gt;&gt;&gt; bytearray('sr_shah','utf_16_le')
  bytearray(b'sr_shah')
  &gt;&gt;&gt; name = 'sr_shah'
  &gt;&gt;&gt; name.encode('windows-1252')
  'sr_shah'
  &gt;&gt;&gt; name.encode('utf_8')
  'sr_shah'
  &gt;&gt;&gt; name.encode('latin_1')
  'sr_shah'
  &gt;&gt;&gt; name.encode('iso-8859-1')
  'sr_shah'
  &gt;&gt;&gt; name.encode('utf-8')
  'sr_shah'
  &gt;&gt;&gt; name.encode('utf-16')
  '\xff\xfes\x00r\x00_\x00s\x00h\x00a\x00h\x00'
  &gt;&gt;&gt; name.encode('utf-16-le')
  's\x00r\x00_\x00s\x00h\x00a\x00h\x00'
  &gt;&gt;&gt; 
  </code></pre>
  
  <p>Please help me to get the right conversion.</p>