Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create byte[] in Jython

I am developing with Jython and I need to use a Java method that requires a byte[] as a parameter.

I tried:

def randomBytesArray(length):
    data = []
    for _ in xrange(length):
        data.append(chr(random.getrandbits(8)))
    methodThatNeedsBytesArrays(data)

But I get this error:

TypeError: methodThatNeedsBytesArrays(): 1st arg can't be coerced to byte[]
like image 288
iomartin Avatar asked Jul 30 '12 18:07

iomartin


People also ask

Can you print [] byte?

You can simply iterate the byte array and print the byte using System. out. println() method.

What is byte [] in Java?

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

What is byte array in Python?

Python bytearray() Function The bytearray() function returns a bytearray object. It can convert objects into bytearray objects, or create empty bytearray object of the specified size.

What is byte array format?

A byte array is simply an area of memory containing a group of contiguous (side by side) bytes, such that it makes sense to talk about them in order: the first byte, the second byte etc..


1 Answers

Sometimes you need to pass a byte array to a function so that the function will fill the byte array with a result. In that case, sending a Python string won't work because Python strings are immutable. Instead, create a Java byte array with the jarray module:

import jarray
bytes = jarray.zeros(100, "b")
length = zlibDeflater.deflate(bytes)
...
like image 103
Jim Pivarski Avatar answered Sep 18 '22 11:09

Jim Pivarski