The first implementation costs 650 gas and the second costs 670 gas (aprox.) I implemented both inside a pure public function that takes an address and returns the bytes32. So to answer your question the 1 is cheaper Answer from Grouchy_Home_7856 on reddit.com
🌐
The Hot Code
thehotcode.com › the hot code › solidity
Transform an address in a bytes32 or uint256 in Solidity | The Hot Code
April 30, 2023 - SolidityTransform an address in a bytes32 or uint256 in Solidity · April 30, 2023 · 1 min · 01 · Transform address into uint256 · 02 · Transform address into bytes32 · 03 · Why is it useful? Knowing how to alter the value of Solidity’s variable is helpful as an audit contractor or hacker.
Discussions

ethereum - How to convert address type to bytes32 via web3? - Stack Overflow
I have contract which require input in bytes32, so I need convert address to bytes32, but no see this method in web3. More on stackoverflow.com
🌐 stackoverflow.com
Return bytes32 or address in one or two functions? - Feedback - Solidity Forum
I have a question in solidity regarding different methods concerned with returning an address that is either as it is, an (address) type or encrypted therefore resulting as (bytes32). If I can just create a quick example to try and get across what I mean. Pls excuse the crude example. More on forum.soliditylang.org
🌐 forum.soliditylang.org
0
June 9, 2022
solidity - Can you convert my address (bytes20) type to a bytes32 string? - Ethereum Stack Exchange
I am running into an error for: TypeError: Type address payable is not implicitly convertible to expected type bytes32. Due to the fact that I am using my address More on ethereum.stackexchange.com
🌐 ethereum.stackexchange.com
July 22, 2021
solidity - Can I pass address type to bytes32 type? - Ethereum Stack Exchange
It looks like sometimes address can be accepted for bytes32 type argument. Is implicit conversion possible? More on ethereum.stackexchange.com
🌐 ethereum.stackexchange.com
July 20, 2021
🌐
Solidity
docs.soliditylang.org › en › latest › types.html
Types — Solidity 0.8.36-develop documentation
If you want to use string parameters or other types that are not implicitly convertible to bytes, you need to convert them to bytes or bytes1/…/bytes32 first. ... // SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.12; contract C { string s = "Storage"; function f(bytes calldata bc, string memory sm, bytes16 b) public view { string memory concatString = string.concat(s, string(bc), "Literal", sm); assert((bytes(s).length + bc.length + 7 + bytes(sm).length) == bytes(concatString).length); bytes memory concatBytes = bytes.concat(bytes(s), bc, bc[:2], "Literal", bytes(sm), b); assert((bytes(s).length + bc.length + 2 + 7 + bytes(sm).length + b.length) == concatBytes.length); } }
🌐
Solidity Forum
forum.soliditylang.org › feedback
Return bytes32 or address in one or two functions? - Feedback - Solidity Forum
June 9, 2022 - Hello everyone. I have a question in solidity regarding different methods concerned with returning an address that is either as it is, an (address) type or encrypted therefore resulting as (bytes32). If I can just crea…
Find elsewhere
🌐
Metana
metana.io › metana: coding bootcamp | software, web3 & cyber › blockchain › what is bytes32 in solidity? - metana
What is Bytes32 in Solidity? - Metana
March 14, 2025 - Here, the Ethereum address is cast into a Bytes32 type. This can be beneficial when working with large datasets or minimizing gas consumption. When should you choose Bytes32 over other data types like string, bytes, or uint?
🌐
OpenZeppelin
forum.openzeppelin.com › support
How to take symbol like USD as bytes32 in solidity? - Support - OpenZeppelin Forum
June 24, 2021 - help needed. mapping(bytes32 => address) tokenSymboltoAddress; 1. The function saves tokenAddress on the tokenSymbol key in a mapping. function addAddresses(bytes32 _name, address _tokenAddress ) { tokenSymboltoAddress[bytes32] = _tokenAddress; ...
Top answer
1 of 2
3

If your goal is to convert your bytes to a bytes32[], maybe you can try something like:

function bytesToBytes32Array(bytes memory data)
    public
    pure
    returns (bytes32[] memory)
{
    // Find 32 bytes segments nb
    uint256 dataNb = data.length / 32;
    // Create an array of dataNb elements
    bytes32[] memory dataList = new bytes32;
    // Start array index at 0
    uint256 index = 0;
    // Loop all 32 bytes segments
    for (uint256 i = 32; i <= data.length; i = i + 32) {
        bytes32 temp;
        // Get 32 bytes from data
        assembly {
            temp := mload(add(data, i))
        }
        // Add extracted 32 bytes to list
        dataList[index] = temp;
        index++;
    }
    // Return data list
    return (dataList);
}
2 of 2
1

The data 0x793e39cd00000000000000000000000064a436ae831c1672ae81f674cab8b6775df3475c0000000000000000000000004a6bc4e803c62081ffebcc8d227b5a87a58f1f8f000000000000000000000000c4375b7de8af5a38a93548eb8453a498222c4ff20000000000000000000000000000000000000000000000001b9de674df070000

is 132 bytes long, and bytes32 can only hold 32 bytes of data. The code you found is working correctly, but it discards all the data after the first 32 bytes because it's impossible to fit that in a bytes32.

If you're looking for converting into an array (bytes32[]), then Phoax's code may be helpful; if you really need it to be bytes32 you might want to look at the data closely, as it looks like it's a couple of different pieces of data packed together and padded mostly to 32 or 64 bytes. From the context, you might only need one of them to be passed as a bytes32.

🌐
Solidity
docs.soliditylang.org › en › v0.6.1 › types.html
Types — Solidity 0.6.1 documentation
If you convert a type that uses a larger byte size to an address, for example bytes32, then the address is truncated. To reduce conversion ambiguity version 0.4.24 and higher of the compiler force you make the truncation explicit in the conversion.
🌐
Foundry
book.getfoundry.sh › reference › cast › cast-to-bytes32
foundry - Ethereum Development Framework
Foundry is a smart contract development toolchain. It manages your dependencies, compiles your project, runs tests, deploys, and lets you interact with the chain from the command-line and via Solidity scripts.
🌐
OpenZeppelin
forum.openzeppelin.com › smart contracts
How to generate 32 bytes number from address and string - Smart Contracts - OpenZeppelin Forum
June 7, 2022 - // SPDX-License-Identifier: MIT pragma solidity 0.8.13; contract Sample { function get() external pure returns(bytes12, bytes32) { string memory str = '1'; address addr = 0x63cDDcF737f3AD13492e8fa59C2D923C9cFAEf14; bytes12 temp = bytes12(ke...
🌐
Readthedocs
solidity.readthedocs.io › en › v0.5.3 › types.html
Types — Solidity 0.5.3 documentation
June 9, 2022 - If you can limit the length to a certain number of bytes, always use one of the value types bytes1 to bytes32 because they are much cheaper. ... If you want to access the byte-representation of a string s, use bytes(s).length / bytes(s)[7] = 'x';. Keep in mind that you are accessing the low-level ...
🌐
GitHub
github.com › ethereum › solidity-examples › blob › master › src › bytes › Bytes.sol
solidity-examples/src/bytes/Bytes.sol at master · ethereum/solidity-examples
function toBytes(address self) internal pure returns (bytes memory bts) { bts = toBytes(bytes32(uint(self) << 96), 20); } ·
Author   ethereum
🌐
Solidity
docs.soliditylang.org › en › v0.8.12 › types.html
Types — Solidity 0.8.12 documentation
April 14, 2022 - If you want to use string parameters or other types that are not implicitly convertible to bytes, you need to convert them to bytes or bytes1/…/bytes32 first. ... // SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.12; contract C { string s = "Storage"; function f(bytes calldata bc, string memory sm, bytes16 b) public view { string memory concat_string = string.concat(s, string(bc), "Literal", sm); assert((bytes(s).length + bc.length + 7 + bytes(sm).length) == bytes(concat_string).length); bytes memory concat_bytes = bytes.concat(bytes(s), bc, bc[:2], "Literal", bytes(sm), b); assert((bytes(s).length + bc.length + 2 + 7 + bytes(sm).length + b.length) == concat_bytes.length); } }
🌐
GitHub
gist.github.com › ageyev › 779797061490f5be64fb02e978feb6ac
StringsAndBytes.sol · GitHub
You can do now in SOLIDITY 8.0.0 Bytes32 to String like this: string memory str = string(abi.encodePacked(bytes32));