0

I am new to the development of smart contract using web3j. I am facing issue, while converting Bytes32[] returned from wrapper of contract(.sol) method to array.

I have contract as:

pragma solidity ^0.4.4;
contract Person
{
    Person[] public people;

    struct Person
    {
        bytes32 firstName;
        bytes32 lastName;

    }

    function addPerson(bytes32 _firstName, bytes32 _lastname) returns (bool success)
    {
        Person memory newPerson;

        newPerson.firstName = _firstName;
        newPerson.lastName = _lastname;


        people.push(newPerson);
        return true;
    }

    function getPeople() constant returns (bytes32[], bytes32[])
    {
        uint leng = people.length;

        bytes32[] memory first_names = new bytes32[](leng);
        bytes32[] memory last_names = new bytes32[](leng);


        for (uint i = 0; i < people.length; i++ )
        {
            Person memory currentPerson;
            currentPerson = people[i];
            first_names[i] = (currentPerson.firstName);
            last_names[i] = (currentPerson.lastName);


        }
        return (first_names,last_names);
    }
}


Function from generated java wrapper as:




    public RemoteCall<Tuple2<List<byte[]>, List<byte[]>>> getPeople() {
        final Function function = new Function("getPeople", 
                Arrays.<Type>asList(), 
                Arrays.<TypeReference<?>>asList(new TypeReference<DynamicArray<Bytes32>>() {}, new TypeReference<DynamicArray<Bytes32>>() {}));
        return new RemoteCall<Tuple2<List<byte[]>, List<byte[]>>>(
                new Callable<Tuple2<List<byte[]>, List<byte[]>>>() {
                    @Override
                    public Tuple2<List<byte[]>, List<byte[]>> call() throws Exception {
                        List<Type> results = executeCallMultipleValueReturn(function);;
                        return new Tuple2<List<byte[]>, List<byte[]>>(
                                (List<byte[]>) results.get(0).getValue(), 
                                (List<byte[]>) results.get(1).getValue());
                    }
                });
    }

In my main function, I am trying access Bytes of Array returned from getPeople method. But I am not note able to converted Bytes of array to string. I am storing tuples values in main method as:

Tuple2<List<byte[]>, List<byte[]>> x = contract.getPeople().send();

I want to convert value, x.getValue1() to string. Can anyone please tell me how to do it.

1 Answer 1

0

Pass your hex or byte in this method will return you the string..

var hexToUtf8 = function(hex) { if (!isHexStrict(hex)) throw new Error('The parameter "'+ hex +'" must be a valid HEX string.');

var str = "";
var code = 0;
hex = hex.replace(/^0x/i,'');

// remove 00 padding from either side
hex = hex.replace(/^(?:00)*/,'');
hex = hex.split("").reverse().join("");
hex = hex.replace(/^(?:00)*/,'');
hex = hex.split("").reverse().join("");

var l = hex.length;

for (var i=0; i < l; i+=2) {
    code = parseInt(hex.substr(i, 2), 16);
    // if (code !== 0) {
    str += String.fromCharCode(code);
    // }
}

return utf8.decode(str);

};

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.