1

Upon reviewing prior questions and answers, I understand that "bytes" should be used for raw byte data of arbitrary length and "string" for UTF-8 data of arbitrary length. I also understand that if I have an input parameter of type "bytes", the input should be in the format "["0x4a", "0x69", "0x6d", ..."0x20"]" (without the quotes).

However, to enter data means converting it to bytes first, and it also means that bytes data is returned through getter functions. I know using "bytes" costs less gas and is easier for the VM to read, but how is this dealt with in the interface?

For example, in the following code, I have changed the input "_custname" from a string type to bytes32, as it is reasonable to limit the length of a person's name. But this means I must enter their name in hex/unicode format, and retrieve it in hex/unicode as well. Would I be better off using a string here? Or perhaps I should add another function that converts it back to string data (though this seems like it would cost more gas than simply starting with string)? Do people typically use bytes in Solidity, then have it converted on the Javascript front end?

uint8 public CarOwnerID;
Customer[] public carowners;
mapping(address => uint8) carAddrIndex;
mapping(string => uint8) carLicIndex;


function newCarOwner(address _address, bytes32 _custname, string _color, string _make, uint16 _year, string _license, bool) onlyMember public {

    carAddrIndex[_address] = CarOwnerID;
    carLicIndex[_license] = CarOwnerID;

    carowners.push(Customer(_address, _custname, _color, _make, _year, _license, true));  

    CarOwnerID ++;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.