Not sure if you still need solution but as it this is still unanswered and for other who may need.
str.getBytes()
only returns byte of that string but actual process is but long.
The main problem is Bytes32(byte[])
only supports 32 length byte[]
.
Also keep in mind Numeric.hexStringToByteArray(strValueInHex)
converts any Hex value of String to byte[]
.
This is the process
Note: "00"
= 1 length Hex & 2 length String
String => Hex => 32 length Hex (ie. 64 length HexString) => byte[] => Bytes32
Here is How I achieved String to Bytes:
String to 64 length HexString:
// 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"));
}
64 length HexString to 32 length byte[]:
byte[] myStringInByte = Numeric.hexStringToByteArray(asciiToHex("myString"));
32 length byte[] to Bytes32:
Bytes32 myStringInBytes32 = new Bytes32(myStringInByte);