简体   繁体   中英

java's getByte() equivalent in python

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.

private static byte[] convert(String str) {
        byte[] byteArray = str.getBytes();
        return byteArray;
    }

convert("sr_shah") results in a byte array like this 115 114 95 115 104 97 104 . On using Charset.defaultCharset() i came to know that my machine's dfault charectorset is windows-1252 .

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.

The methods i used are bytearray(),bytes(),str.encode() . I used encodings like windows-1252,utf_16,utf_8,utf_16_le,utf_16_be,iso-8859-1 unfortunately none of them give the right result as i expected(ie 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.

>>> bytearray('sr_shah','windows-1252')
bytearray(b'sr_shah')
>>> bytearray('sr_shah','utf_8')
bytearray(b'sr_shah')
>>> bytearray('sr_ahah','utf_16')
bytearray(b'sr_ahah')
>>> bytearray('sr_shah','utf_16_le')
bytearray(b'sr_shah')
>>> name = 'sr_shah'
>>> name.encode('windows-1252')
'sr_shah'
>>> name.encode('utf_8')
'sr_shah'
>>> name.encode('latin_1')
'sr_shah'
>>> name.encode('iso-8859-1')
'sr_shah'
>>> name.encode('utf-8')
'sr_shah'
>>> name.encode('utf-16')
'\xff\xfes\x00r\x00_\x00s\x00h\x00a\x00h\x00'
>>> name.encode('utf-16-le')
's\x00r\x00_\x00s\x00h\x00a\x00h\x00'
>>> 

Please help me to get the right conversion.

You can do this:

str = 'sr_shah'
b = [ord(s) for s in str]
print b

**Output**

[115, 114, 95, 115, 104, 97, 104]

the ord() built-in function is as close as I know to the getByte() function you want, although it works on single characters, so you need to deal with the arrays yourself.

The bytearray you have created in Python contains the bytes you want. To see their decimal representation, print the bytes one by one:

>>> for x in bytearray('sr_shah','windows-1252'): print(x)
...
115
114
95
115
104
97
104

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM