How to take symbol like USD as bytes32 in solidity?

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;


2. Returns the address added in the mapping on the given token symbol.

function getAddress(bytes32 name) returns (address) {
	return tokenSymboltoAddress[name];

Can someone help me how to write or run these functions. How do we write symbol as bytes32 in function argument?

Can we convert bytes32 to string in solidity ?

Hi, welcome! :wave:

I think you can have a search on the google. Here is what I find, you can have a look.
solidity - How to convert bytes32 to string? - Ethereum Stack Exchange

Google search didn’t help with my problem?

I just deployed a contract followed a solution from the answers I shared above, here is the result:

Parameters is: 0x00000000000000000000000054686973206973207465737420636f6e74657874, and the context is just This is test context, it works fine for me.

But what if users have to directly take input in bytes32. So first they have to convert string to bytes32. And then theh can input the data in bytes32.??

Emmm, the type of the function parameter is bytes32, so you should call this function with bytes32, as for user, they can pass a string, and you should convert it to the bytes32, or you can change the type in the contract, just use mapping(string => address) tokenSymboltoAddress;