Hot answers tagged

48

This has worked for me so far. Not sure if it's the best way. function stringToBytes32(string memory source) public pure returns (bytes32 result) { bytes memory tempEmptyStringTest = bytes(source); if (tempEmptyStringTest.length == 0) { return 0x0; } assembly { result := mload(add(source, 32)) } } Also, remember that ...


47

Why string instead of bytes32? Use string for arbitrary-length string (UTF-8) data that's longer than 32 bytes. Frontends can decode a long string easier using methods like web3.toAscii or UTF-8 (when issues are fixed), instead of implementing the logic of UTF-8 decoding a series of bytes32. From Solidity docs: As a rule of thumb, use bytes for ...


31

2 main reasons: Contracts currently cannot read a string that's returned by another contract. The EVM has a word-size of 32 bytes, so it is "optimized" for dealing with data in chunks of 32 bytes. (Compilers, such as Solidity, have to do more work and generate more bytecode when data isn't in chunks of 32 bytes, which effectively leads to higher gas cost.)


30

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++; } } ...


25

Bytes is a dynamic array of bytes. It's shorthand for byte[] and you'll see examples of a bytes being treated as an array in code from time to time. myByte[x]. It can have a length of zero and you can do things like append a byte to the end. Bytes32 is exactly 32 bytes long. It takes exactly one 32-byte word to represent a bytes32 because there's no need ...


16

Each bytes32 can store up to 32 letters (ASCII): each character is a byte.


16

string is not equal to bytes32 but it is equal to bytes, because its length is dynamic. so you could use a casting bytes B=bytes(S); //S string E.g contract string_test { function string_tobytes( string s) constant returns (bytes){ bytes memory b3 = bytes(s); return b3; } } The conversion of string to bytes32 is possible only ...


15

Like the other post said, you only want to use strings for dynamically allocated data, otherwise Byte32 is going to perform better. Bytes32 is also going to be better in gas. If you want to play around with it, I built a little fiddle of it https://ethfiddle.com/70ipaEIFdk Byte used 21465 gas String used 21897 gas pragma solidity ^0.4.18; contract ...


15

Try using the web3.js function web3.toAscii (doc), which might change name to web3.toUtf8 or something similar in the future.


14

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. For versions 0.5.0+ please use (tested from 0.5 to 0.7.2 - it is ...


11

You can use web3.toAscii(hexString) as documented in Web3 JavaScript Ðapp API: > web3.toAscii("0x4d61726b65745061792e696f206973206465706c6f79696e6720536d61727420") "MarketPay.io is deploying Smart " You can also use web3.toUtf8(hexString): > web3.toUtf8("0x4d61726b65745061792e696f206973206465706c6f79696e6720536d61727420") "MarketPay.io is deploying ...


11

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 (...


9

It is possible to do this with assembly: pragma solidity ^0.4.8; contract c { event trace(bytes32 x, bytes16 a, bytes16 b); function foo(bytes32 source) { bytes16[2] memory y = [bytes16(0), 0]; assembly { mstore(y, source) mstore(add(y, 16), source) } trace(source, y[0], y[1]); } } For ...


8

The bytes32 type is always exactly 32 bytes. Therefore, its length is always 32. It is unaware of whether it contains a string, number or something else. I would recommend doing: require(_name[0] != 0); to verify that it does not represent an empty string.


8

If you have MetaMask installed, then the following works in the browser console: > web3.padRight(web3.fromAscii('hello'), 34) "0x68656c6c6f0000000000000000000000"


7

You can simply cast a bytes32 to uint with uint(number1).


6

Always use quotes. JavaScript has a maximum integer precision of 64 bits. This means that if your number is over 8 bytes, and you type it in without quotes, you will immediately lose precision. For example: 0x1122334455667700 - 1 > 0x1122334455667700 0x1122334455667700 - 0x1122334455667701 > 0 The correct way: web3.toBigNumber("...


6

I have test in this site https://ethfiddle.com/zLxE5Y-8B4 contract TestGas { string constant statictext = "Hello World"; bytes11 constant byteText11 = "Hello World"; bytes32 constant byteText32 = "Hello World"; function getString() payable public returns(string){ return statictext; } function getByte11() payable public ...


6

Or just do away with your number1 variable completely, while also remembering to change the return type: function getTest1() constant returns (uint) { return uint(lastblockhashused) & 0xfff; }


6

For solidity 0.5.x you can use pragma solidity ^0.5.0; contract Test { function test(bytes32 data) external pure returns (address) { return address(uint160(uint256(data))); } } First convert the bytes32 to a uint256, later to uint160(20 bytes) and finaly to addres, this use big endian. If you want use little endian you should use address(...


5

As of feb 2021 you can do bytes32 foo = "hello"; string memory bar = string(abi.encodePacked(foo));


5

72370055773322622139731865630429942408293740416025352524660990004945706024960 is 0xa000000000000000000000000000000000000000000000000000000000000000L - or 10 in hex, left shifted. Without seeing your web3 code, it's difficult to say why it was parsed this way, but it seems likely web3 treated your input as a single byte byte string, and left-aligned it into ...


5

Your question is not clear enough. When you want to split 32 bytes into 5 variables that have a total length of 2+2+2+2+8+1 = 17 you should specify what bytes you want to be chunked (bytes from left or bytes from right). I wrote an example that solves your problem as far as I could understand it. Comment the answer - explain what you exactly need, and I'll ...


5

Online GUI here for converting bytes32 to string and the other way: https://blockchangers.github.io/solidity-converter-online/


5

A comparison between two bytes32 IS possible in solidity. Consider this simple contract: pragma solidity 0.5.4; contract Test { bytes32 public constant bytes32_ = "Hello World!"; bytes32 public constant anotherBytes32 = "Hello World!"; function areTheyEqual() public pure returns(bool) { return (bytes32_ == anotherBytes32); } } ...


4

try this snipet 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++; } } ...


4

I found a solution using inline assembly: contract cutByte32 { //"0xa9c40ddcb43ebbc83add97b8f9f361f12b19bceff2f76b68f66b5bb1812365a9" //use this as remix command function cut(bytes32 sha) constant returns (bytes16 half1, bytes16 half2) { assembly { let freemem_pointer := mload(0x40) mstore(add(freemem_pointer,0x00), sha) half1 :...


4

Replace ['Sat', 'Vit'] with: ['Sat', 'Vit'].map(x => web3.fromAscii(x)) if you're on Truffle 4.x (web3 0.x) ['Sat', 'Vit'].map(x => web3.utils.asciiToHex(x)) if you're on Truffle 5.x (web3 1.x)


4

If you're on web3 v0.x, then: Use web3.fromAscii before you send the string to the smart contract Use web3.toAscii after you read the string from the smart contract If you're on web3 v1.x, then: Use web3.utils.asciiToHex before you send the string to the smart contract Use web3.utils.hexToAscii after you read the string from the smart contract


3

From the ABI: bytes is right-padded with zeroes (to a length of 32). "0x123abc" with quotes is bytes. types like bytes32, uint, int are left-padded with zeroes (to a length of 32). 0x123abc without quotes is a number (in base 16). Since the contract takes bytes32, use 0x123abc without quotes or its decimal equivalent 1194684 to reduce confusion with hex. ...


Only top voted, non community-wiki answers of a minimum length are eligible