30

How can I convert a bytes32 to a string? Does anyone has a magic function or library which does it?

7

Based on the latest compiler version 0.4.24, I use the following.

function convertingToString()public returns(string){
bytes32 memory hw = "Hello World";
string memory converted = string(hw);
return converted;
}

Using explicit conversion to carry it out. The reverse is also possible.

29

Here's one:

function bytes32ToString(bytes32 x) constant returns (string) {
    bytes memory bytesString = new bytes(32);
    uint charCount = 0;
    for (uint j = 0; j < 32; j++) {
        byte char = byte(bytes32(uint(x) * 2 ** (8 * j)));
        if (char != 0) {
            bytesString[charCount] = char;
            charCount++;
        }
    }
    bytes memory bytesStringTrimmed = new bytes(charCount);
    for (j = 0; j < charCount; j++) {
        bytesStringTrimmed[j] = bytesString[j];
    }
    return string(bytesStringTrimmed);
}

To test, here it is combined with how to concatenate an array of bytes32. Paste the following in Remix.

contract C {
    function bytes32ToString(bytes32 x) constant returns (string) {
        bytes memory bytesString = new bytes(32);
        uint charCount = 0;
        for (uint j = 0; j < 32; j++) {
            byte char = byte(bytes32(uint(x) * 2 ** (8 * j)));
            if (char != 0) {
                bytesString[charCount] = char;
                charCount++;
            }
        }
        bytes memory bytesStringTrimmed = new bytes(charCount);
        for (j = 0; j < charCount; j++) {
            bytesStringTrimmed[j] = bytesString[j];
        }
        return string(bytesStringTrimmed);
    }

    function bytes32ArrayToString(bytes32[] data) returns (string) {
        bytes memory bytesString = new bytes(data.length * 32);
        uint urlLength;
        for (uint i=0; i<data.length; i++) {
            for (uint j=0; j<32; j++) {
                byte char = byte(bytes32(uint(data[i]) * 2 ** (8 * j)));
                if (char != 0) {
                    bytesString[urlLength] = char;
                    urlLength += 1;
                }
            }
        }
        bytes memory bytesStringTrimmed = new bytes(urlLength);
        for (i=0; i<urlLength; i++) {
            bytesStringTrimmed[i] = bytesString[i];
        }
        return string(bytesStringTrimmed);
    }    
}

Click "Create". Then in bytes32ToString field enter "0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" and click bytes32ToString.

In bytes32ArrayToString field enter ["0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"] and click bytes32ArrayToString.

Both will show same result (ABI encoding): Result: "0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"


Here's another example. In bytes32ToString field enter "0xc3b6" and click bytes32ToString. You will get ö.

enter image description here

  • I used the bytes32ToString function from above, but I obviously doesn't work with special characters such as ö ä ü etc. Is there a way to use this function with special characters? – Bumblebee Apr 20 '17 at 10:02
  • @Bumblebee I added an example for ö and it works. If still needed, probably ask a separate question. (The input or decoding the output might be your problem.) – eth Apr 21 '17 at 2:31
  • I am trying your bytes32ArrayToString with a fixed bytes32[10], but it seems I throw an invalid opcode if I use 8-10 full slots of the array. Meaning I have a test for abcdefghijklmnopqrstuvwxyzabcdef (32 characters) 10 times in an array and pass that in, but it throws. If I have 7 or less it works. Any idea why? – The Nomad Nov 12 '17 at 12:49
  • @TheNomad Not sure. Suggestions, try once instead of 10 times, and post a new question (a Remix or ethfiddle link might help). – eth Nov 19 '17 at 19:37
  • 1
    @Russo It's not clear what you're trying to do in Python; Stackoverflow is a better fit for questions about converting from different Python types. – eth Jun 3 '18 at 17:13
9

How to convert a bytes32 to string:

pragma solidity ^0.4.15;

contract Bytes32ToString {

function bytes32ToStr(bytes32 _bytes32) public pure returns (string) {

    // string memory str = string(_bytes32);
    // TypeError: Explicit type conversion not allowed from "bytes32" to "string storage pointer"
    // thus we should fist convert bytes32 to bytes (to dynamically-sized byte array)

    bytes memory bytesArray = new bytes(32);
    for (uint256 i; i < 32; i++) {
        bytesArray[i] = _bytes32[i];
        }
    return string(bytesArray);
    }

}
0

Here's how I'm doing it:

function char(byte b) returns (byte c) {
    if (b < 10) return byte(uint8(b) + 0x30);
    else return byte(uint8(b) + 0x57);
}


function bytes32string(bytes32 b32) returns (string out) {
    bytes memory s = new bytes(64);

    for (var i = 0; i < 32; i++) {
        byte b = byte(b32[i]);
        byte hi = byte(uint8(b) / 16);
        byte lo = byte(uint8(b) - 16 * uint8(hi));
        s[i*2] = char(hi);
        s[i*2+1] = char(lo);            
    }

    out = string(s);
}

Your Answer

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

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