Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert java byte[] to python string?

I know that java and python handle bytes differently so I am a little bit confused about how to convert byte[] to python string I have this byte[] in java

{ 118, -86, -46, -63, 100, -69, -30, -102, -82, -44, -40, 92, 0, 98, 36, -94 }

I want to convert it to python string here is how i did it

b=[118, -86, -46, -63, 100, -69, -30, -102, -82, -44, -40, 92, 0, 98, 36, -94]
str=""
for i in b:
    str=str+chr(abs(i))

But I am not really sure if this is the correct way to do it.

like image 804
Minato Avatar asked Dec 03 '16 18:12

Minato


People also ask

Is byte [] same as string?

Byte objects are sequence of Bytes, whereas Strings are sequence of characters. Byte objects are in machine readable form internally, Strings are only in human readable form. Since Byte objects are machine readable, they can be directly stored on the disk.

How do you turn a byte into a string?

One method is to create a string variable and then append the byte value to the string variable with the help of + operator. This will directly convert the byte value to a string and add it in the string variable.

What is byte [] in Java?

The byte keyword is a data type that can store whole numbers from -128 to 127.


2 Answers

The Java byte type is a signed integer; the value ranges between -128 and 127. Python's chr expects a value between 0 and 255 instead. From the Primitive Data Types section of the Java tutorial:

byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive).

You need to convert from 2s compliment to an unsigned integer:

def twoscomplement_to_unsigned(i):
    return i % 256

result = ''.join([chr(twoscomplement_to_unsigned(i)) for i in b])

However, if this is Python 3, you really want to use the bytes type:

result = bytes(map(twoscomplement_to_unsigned, b))
like image 163
Martijn Pieters Avatar answered Oct 15 '22 06:10

Martijn Pieters


Assuming you're using Python 3, bytes can already be initialized from a list. You'll need to convert the signed integers to unsigned bytes first.

items = [118, -86, -46, -63, 100, -69, -30, -102, -82, -44, -40, 92, 0, 98, 36, -94]
data = bytes(b % 256 for b in items)
print(data)  # b'v\xaa\xd2\xc1d\xbb\xe2\x9a\xae\xd4\xd8\\\x00b$\xa2'

If the bytes represent text, decode it afterwards. In your example, they do not represent text encoded to UTF-8, so this would fail.

data = data.decode('utf8')
print(data)
like image 21
davidism Avatar answered Oct 15 '22 08:10

davidism