Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are java equivalents to JS push, concat, unshift?

I'm trying to convert the following JS to Java:

serializeSig:function(r,s){
var rBa=r.toByteArraySigned();
var sBa=s.toByteArraySigned();
var sequence=[];
sequence.push(2);
sequence.push(rBa.length);
sequence=sequence.concat(rBa);
sequence.push(2);
sequence.push(sBa.length);
sequence=sequence.concat(sBa);
sequence.unshift(sequence.length);
sequence.unshift(48);
return sequence
}

I think push would translate to add, concat to some kind of addAll, but what is unshift? And of what type would my variables in java be?

like image 489
membersound Avatar asked Oct 29 '25 05:10

membersound


1 Answers

myArray.unshift(obj) corresponds to myList.add(0, obj).

A byte array in Java is byte[]. A list of byte arrays would be a List<byte[]>. (That's java.util.List, not java.awt.List, just in case you get the wrong import.)

EDIT: Looks like you are trying to create a byte array, not a list of byte arrays. In that case, you should use a java.nio.ByteBuffer, or possibly a java.io.ByteArrayOutputStream. The latter has to be written to sequentially -- you cannot do the equivalent of an unshift.

like image 186
Eric Galluzzo Avatar answered Oct 31 '25 20:10

Eric Galluzzo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!