I am sending ETH to a contract as follows:

web3.eth.sendTransaction({from: sender, to: address, value:amount, gas:300000, data:"1234"});

In my fallback function, I can see msg.data has the following value (as expected):


The trouble is (and I've been at this for hours...) how do I convert this into a uint holding the value 1234?

Here's my attempt that sorta works (i.e. doesn't):

function bytes2uint(bytes _data) constant returns(uint){
    uint i=0;
    uint dec=0;

    for (i = 0; i < _data.length; i++) {
        byte e=_data[_data.length-i-1];
        uint f=uint(e);
    return dec;

I am able to successfully convert "0x1234", "0x123456", "0x12", etc. (i.e. an even number of digits). However, when I try "0x123" or "0x12345" (for example), the trailing digit is somehow getting truncated?

1 Answer 1


You just use


Converting to other types is also supported https://github.com/ethereum/wiki/wiki/JavaScript-API

  • I'm trying to make it as "easy" as possible for my users to interact with my contract. web3.toHex or web3.fromDecimal is verbose and I was trying to find a way of omitting them.
    – Eamorr
    Mar 24, 2016 at 23:40

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.