java.lang.UnsupportedOperationException: Input byte array must be in range 0 < M <= 32 and length must match type

I am trying to do as given below. I wanted to add the people but wrapper generated in web3j throwing the error above:

String s = "Rahul"; 
Bytes32 b1=new Bytes32(s.getBytes());
contract.addPerson(b1,new Uint256(new BigInteger("35"));
  • Can you add a bit more information in the support of your question?
    – Aniket
    Commented Feb 3, 2017 at 13:24
  • 1
    String s = "Rahul"; System.out.println(s.getBytes().length); Bytes32 b1=new Bytes32(s.getBytes()); System.out.println("Add person --"+contract.addPerson(b1,new Uint256(new BigInteger("35"))));
    – Rahul
    Commented Feb 6, 2017 at 6:50
  • 1
    I dont completely understand your question. However, if you want to work with Strings, Solidity also has a data type "string" which converts into a "Utf8String" in Web3J. Does that help? Commented Feb 6, 2017 at 14:22
  • My requirement is to convert Java string to byte32. Here i wanted to send the byte32 as an input to my contract.
    – Rahul
    Commented Feb 9, 2017 at 12:15
  • Rahul, did u find a solution? Commented Apr 12, 2017 at 7:40

3 Answers 3


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);
  • I have wrote shorter method to do same task on my own question here.
    – wannamit
    Commented Aug 3, 2017 at 10:21

It is best to use the JVM runtime library whenever possible; that code is usually of high quality, although exceptions do exist. In the following code I first create a byte[], then derive a hex value by calling DatatypeConverter.printHexBinary.

byte[] byteArray = "This string is converted to a byte array".getBytes();
String hexValue = javax.xml.bind.DatatypeConverter.printHexBinary(byteArray);
  • What is the vice versa of this code? I mean converting string from Byte32.
    – kaushik_pm
    Commented Oct 12, 2018 at 13:34

byte[] byte = Numeric.hexStringToByteArray("String Value");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.