Created
November 23, 2021 20:56
Revisions
-
tangert created this gist
Nov 23, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,43 @@ function getAddressChars(address _address) private pure returns (bytes memory) { bytes memory hexSymbols = "0123456789abcdef"; uint256 value = uint256(uint160(_address)); uint256 temp = value; uint256 length = 0; while (temp != 0) { length++; temp >>= 8; } uint256 bufferLength = 2 * length + 2; bytes memory chars = new bytes(bufferLength); // // first two chars will always be 0x. chars[0] = "0"; chars[1] = "x"; // // first fill up the buffer // start at 2 because we already know that the first two characters are 0x. for (uint256 i = 2; i < bufferLength; i++) { // for whatever reason, this fills up the buffer starting from the end // so here we are incrementing up but filling in the end of the array first. // if someone knows how to fix this chars[bufferLength - i + 1] = hexSymbols[value & 0xf]; value >>= 4; } return chars; } /* Usage: bytes memory chars = getAddressChars(_address); // now you can access each character of an address directly through an index. // so string(char[0]) returns "0", string(char[1]) returns "x", etc. */