Skip to content

Instantly share code, notes, and snippets.

@D-Nice
Created December 25, 2016 20:41
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save D-Nice/00da3124c2588b0eda839d8396b6c592 to your computer and use it in GitHub Desktop.
Save D-Nice/00da3124c2588b0eda839d8396b6c592 to your computer and use it in GitHub Desktop.
Solidity inline assembly code for converting a hex string into the same bytes32
/* Converts a hexadecimal string, whose bytes equivalent length
can be up to 32 bytes (at least only tested and meant for up to 32 bytes)
into a bytes32 type.
EXAMPLE I/O
I: "a0c3689df9ce9c3aee5c1ba34dd1649098e2a10f6b2b337bf26695318cc0b958"
O: bytes32: 0xa0c3689df9ce9c3aee5c1ba34dd1649098e2a10f6b2b337bf26695318cc0b958
Currently assumes the 0x prefix is omitted from the string.
Written in ASM for low gas footprint
*/
contract hexStringToBytes32 {
function parse(string _m) returns(bytes32) {
bytes memory x = bytes(_m);
uint len = x.length;
delete x;
assembly
{
let n := len
len := sub(len, 1)
let b := 0
let o := 0
let r := 0
loop:
n := sub(n, 1)
div(n, 0x20) 0x20 mul 0x20 add _m add mload
mod(n, 0x20)
byte
=: b
// ensures valid hexadecimal character
jumpi(invalidJumpLabel, lt(b, 0x30)) //can disable these checks to save some gas
jumpi(num, lt(b, 0x3A))
jumpi(invalidJumpLabel, lt(b, 0x41)) //can disable these checks to save some gas
jumpi(ualpha, lt(b, 0x47))
jumpi(invalidJumpLabel, lt(b, 0x61)) //can disable these checks to save some gas
jumpi(lalpha, lt(b, 0x67))
jump(invalidJumpLabel)
num:
o := 0x30
jump(loopcalc)
lalpha:
o := 0x57
jump(loopcalc)
ualpha:
o := 0x37
jump(loopcalc)
loopcalc:
sub(b, o)
sub(len, n) 0x10 exp mul r add
=: r
n 0 eq 1 sub
loop
jumpi
loopend:
0x0
mstore
return(0, 0x20)
}
}
}
@algtm
Copy link

algtm commented Nov 18, 2022

cant complie at 0.8
ParserError: Call or assignment expected.
--> contracts/bytesToString.sol:26:13:
|
26 | loop:
| ^

@D-Nice
Copy link
Author

D-Nice commented Nov 21, 2022

this was written for solc 0.4.x or 0.5 back in the day iirc. A number of these low-level methodologies are not supported anymore in the most recent version of solidity. There is something called Huff which replicates working directly on the stack with EVM opcodes, or utilize the older solc which supports this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment