java.lang.UnsupportedOperationException: la matriz de bytes de entrada debe estar en el rango 0 < M <= 32 y la longitud debe coincidir con el tipo
Estoy tratando de hacer lo que se indica a continuación. Quería agregar a las personas, pero el envoltorio generado en web3j arroja el error anterior:
String s = "Rahul";
//System.out.println(s.getBytes().length);
Bytes32 b1=new Bytes32(s.getBytes());
contract.addPerson(b1,new Uint256(new BigInteger("35"));
Lo mejor es utilizar la biblioteca de tiempo de ejecución de JVM siempre que sea posible; ese código suele ser de alta calidad, aunque existen excepciones. En el siguiente código, primero creo un byte[]
, luego obtengo un valor hexadecimal llamando a DatatypeConverter.printHexBinary .
byte[] byteArray = "This string is converted to a byte array".getBytes();
String hexValue = javax.xml.bind.DatatypeConverter.printHexBinary(byteArray);
No estoy seguro de si aún necesita una solución, pero como esto aún no tiene respuesta y para otros que puedan necesitarla.
str.getBytes()
solo devuelve el byte de esa cadena, pero el proceso real es largo.
El problema principal es que Bytes32(byte[])
solo admite 32 longitudes byte[]
. También tenga en cuenta Numeric.hexStringToByteArray(strValueInHex)
que convierte cualquier valor hexadecimal de String a byte[]
.
este es el proceso
Nota: "00"
= 1 hexadecimal de longitud y 2 cadenas de longitud
String => Hex => 32 length Hex (ie. 64 length HexString) => byte[] => Bytes32
Así es como logré String to Bytes:
Cadena a 64 HexString de longitud:
// String to 64 length HexString (equivalent to 32 Hex lenght)
public static String asciiToHex(String asciiValue)
{
char[] chars = asciiValue.toCharArray();
StringBuffer hex = new StringBuffer();
for (int i = 0; i < chars.length; i++)
{
hex.append(Integer.toHexString((int) chars[i]));
}
return hex.toString() + "".join("", Collections.nCopies(32 - (hex.length()/2), "00"));
}
HexString de 64 longitudes a 32 bytes de longitud[]:
byte[] myStringInByte = Numeric.hexStringToByteArray(asciiToHex("myString"));
32 bytes de longitud [] a Bytes32:
Bytes32 myStringInBytes32 = new Bytes32(myStringInByte);
byte[] byte = Numeric.hexStringToByteArray("String Value");
Aniket
Raúl
Max Binnewies
Raúl
prusty narayan
platicar