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 ?
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.
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.
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.
The following should suffice:
def array = [0, 0, 0, 0, 0] as byte[]
Have a look here for more details on arrays in groovy.
In addition to rich.okelly's answer,
byte[] array = [0, 0, 0, 0, 0]
works as well
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With