0

I was wondering if it is possible to convert a string to a bytes32 or bytes in solidity like this, I want an input in string and require an equivalent bytes32 and bytes both.

input string = "0x8f54f1c2d0eb5771cd5bf67a6689fcd6eed9444d91a39e5ef32a9b4ae5ca14ff" output bytes32 = 0x8f54f1c2d0eb5771cd5bf67a6689fcd6eed9444d91a39e5ef32a9b4ae5ca14ff

input string = "0xd724b8592f7962a584cfedcacc2e308292f19f5245be8bdecb42354277d3a8674266a8d0b0f2fcdcdc42bc295e2bf6378da440ed7bcb022a154c8a28a825ddb301"

output bytes = 0xd724b8592f7962a584cfedcacc2e308292f19f5245be8bdecb42354277d3a8674266a8d0b0f2fcdcdc42bc295e2bf6378da440ed7bcb022a154c8a28a825ddb301

I have already looked into this link: How to convert a bytes32 to string but this is not working and returning different values than above.

10
  • The bytes32 equivalence of a string would be the ASCII encoding of that string (not what you're describing in your question). Sep 5, 2020 at 17:17
  • What's the use case for this? Can't you just make your function take bytes32 in the first place? That would keep the gas usage a lot lower.
    – Morten
    Sep 5, 2020 at 17:18
  • I am okay with the gas usage. I want to provide input as a JSON to a function. The Json contains two values as given below: '{"Data":"0x8f54f1c2d0eb5771cd5bf67a6689fcd6eed9444d91a39e5ef32a9b4ae5ca14ff","Signature":"0xd724b8592f7962a584cfedcacc2e308292f19f5245be8bdecb42354277d3a8674266a8d0b0f2fcdcdc42bc295e2bf6378da440ed7bcb022a154c8a28a825ddb301"}'. I get the values from JSON as string. Now I want to convert it to bytes and bytes32 to pass to another function which accepts bytes and bytes32 as an argument. Sep 5, 2020 at 17:37
  • @goodvibration .. i intend to get values as described in the question. Can you please suggest something. I have already tried available solutions but none of them are working Sep 5, 2020 at 17:41
  • You can use ethereum.stackexchange.com/questions/39989/… to convert an hex string into a bytes array.
    – Ismael
    Sep 5, 2020 at 19:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.