Hot answers tagged

43

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


41

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


30

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


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


22

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


15

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


14

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


14

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


13

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


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


10

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


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.


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.


7

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


7

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


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

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


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


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

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


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


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

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

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

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


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


3

Summary There are some oddities when using bytes32 as function parameters. From Solidity Features - Byte Arrays: PT Basic support for variable-length byte arrays. This includes bytes type for storage variables msg.data is of bytes type and contains the calldata functions taking arbitrary parameters (call, sha3, ...) can be called with bytes arguments. ...


3

This problem is caused because javascript doesn't have a native bytes32 type. And the ambiguity of interpreting strings as bytes32 values. If you want to avoid troubles you have to prefix hexadecimal strings with "0x" and complete to 64 characters padding with zeros where neccesary. Like this for example: ["...


3

It's possible to mutate elements of a fixed-sized array, but not a fixed-sized byte array (like bytes32). So this is valid: byte[32] myBytes; myBytes[3] = 0x5; but this is not valid: bytes32 myBytes; myBytes[3] = 0x5; // TypeError: Expression has to be an lvalue.


3

It is because Javascript uses float to store your numbers: truffle(development)> 0x5b3138302c3132302c3135392c3232332c36342c3135392c36392c32382c322d 4.1247432523778224e+76 truffle(development)> 0x5b3138302c3132302c3135392c3232332c36342c3135392c36392c32382c322c 4.1247432523778224e+76 You want to use BigNumber class and its equals() function: truffle(...


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