Bytes32 – How to Pass Bytes32 to Constructor in Solidity Contract Deployment

apibytes32constructorcontract-deployment

I have s constructor that takes a bytes32 parameter and assign its value to the jobID variable

contract APIConsumer {

    bytes32 public jobId;

    constructor( bytes32 _jobId) {
        jobId = _jobId;
    }
}

But when I pass the value "ec013753fda740f8bc74a966daea0723" in the constructor it assign this value to the jobId variable "0x00000000000000000000000000000000ec013753fda740f8bc74a966daea0723"
I want to know why it adds those zeros and how to fix it (make it assign the same value passed in the parameters and do not add zeros).

Best Answer

This is intended behavior, the value is padded right ( = zeroes are added to the left of your number to fill the entire 32 bytes). I don't think you actually want to "fix" this, but if you do, you could use a byte array instead, like so:

bytes public jobId;

constructor( bytes  memory _jobId) {
    jobId = _jobId;
}
Related Topic