Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

byte array in groovy

in java I can create byte array: byte[] array = new byte[] { 0, 0, 0, 0, 0 }; but this construct is invalid in groovy. How I can create byte array in groovy ?

like image 837
hudi Avatar asked Apr 04 '12 13:04

hudi


People also ask

How to convert byte array to String in Groovy?

To convert a byte[] array to a String we can simply use the new String(byte[]) constructor. But if the array contains non-printable bytes we don't get a good representation. In Groovy we can use the method encodeHex() to transform a byte[] array to a hex String value.

What is array in groovy?

An Array is an object that contains elements of similar data type. Groovy reuses the list notation for arrays, but to make such literals arrays, you need to explicitly define the type of the array through coercion or type declaration. You can also create multi-dimensional arrays.

How do you create a byte array of objects in Java?

String str = new String(byteArray, StandardCharsets. UTF_8); String class also has a method to convert a subset of the byte array to String. byte[] byteArray1 = { 80, 65, 78, 75, 65, 74 }; String str = new String(byteArray1, 0, 3, StandardCharsets.


2 Answers

The following should suffice:

def array = [0, 0, 0, 0, 0] as byte[] 

Have a look here for more details on arrays in groovy.

like image 117
Rich O'Kelly Avatar answered Sep 20 '22 07:09

Rich O'Kelly


In addition to rich.okelly's answer,

byte[] array = [0, 0, 0, 0, 0] 

works as well

like image 33
tim_yates Avatar answered Sep 20 '22 07:09

tim_yates