0

I am trying to take user input strings and convert them to bytes32 and bytes5 for a contract built in Solidity 5.11, I tried 32 and 5 character long random strings respectively however they are not working, is there an inbuilt function for web3 to convert Strings to bytes (either 32 or 5) like web3.utils.fromAscii can be used to generate appropriate addresses from strings?

console.log(addressing("address");
console.log(bitify32("data"));
console.log(bitify5("key"));
function addressing(input) {
    let zeros = "0";  
    let input = web3.utils.fromAscii(input);
    if (input.length < 42) {
        let remainder = 32-input.length;
        for(let i = remainder-1; i > 0; i--) {
            zeros += "0";
        }
    }
    let inputZerod = input+zeros;
    return inputZerod;
}

function bytify32(input) {
    let zeros = "0";  
    let input = web3.utils.fromAscii(input);
    if (input.length < 32) {
        let remainder = 32-input.length;
        for(let i = remainder-1; i > 0; i--) {
            zeros += "0";
        }
    }
    let inputZerod = input+zeros;
    return inputZerod;
}
pragma solidity ^0.5.11;

contract bytesTest{

  mapping(address => BytesStruct) testBytes; 

     struct BytesStruct{
        address stringAddress;
       bytes32 string1;
       bytes5 string2;
     }


     function getByteStrings(address _stringAddress) view public returns(bytes32, bytes5){
         return(testBytes[_stringAddress].string1, testBytes[_stringAddress].string2);
     }


  function addByteString(address _stringAddress, bytes32 _string1, bytes5 _string2) public{
    testBytes[_stringAddress].string1 = _string1;
    testBytes[_stringAddress].string2 = _string2;

  }

}
8
  • Please share your code in order to clarify your question. Jul 3 '20 at 17:35
  • I added my function, I just tried to make the string length match the byte size but it has not worked when trying to fill a 32 byte field in solidity 5.11 Jul 3 '20 at 17:48
  • I don't get it. All you really need is web3.utils.fromAscii(input) (or in its more common name - web3.utils.asciiToHex(input)), which you are already using. Why all the padding? Jul 3 '20 at 18:04
  • I added a breakout of the smart contract for more context. I had to add padding to the addresses to get them to the appropriate length and I was doing the same for the bytes but with or without the padding it makes no difference Jul 3 '20 at 18:25
  • What do you even mean "generate appropriate addresses from strings"??? Converting an ASCII string into bytes32 doesn't make an address out of it in any way. An address is 20 bytes, not 32! Jul 3 '20 at 18:37
1

I am just trying to understand why the bytes32 and 5, need strings of length 66 and 11 respectively if each character is a byte

I'll explain for bytes32:

In order to send bytes32 from web3.js to a smart-contract function, you need to provide a string which starts with "0x" followed by 64 hexadecimal characters.

Why 64? Because each character represents 4 bits (f - the highest - represents 1111).

Since each byte consists of 8 bits, you need 64 hexadecimal characters in order to represent 32 bytes.

Including the "0x" prefix, you get a string of 66 characters.

2
  • If I understand you correctly, my padding for the Bytes was necessary, I was just off on the count, thank you. I was under the impression in ASCII each character was eight bits not four, this helps alot with my confusion thank you very much. Jul 3 '20 at 19:05
  • @user3316323: You're welcome, please accept the answer by clicking on the V next to it if it has answered your question. Jul 3 '20 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.