How to append a string at the beginning of a value with U256 type

Please how can I add a value at the beginning of a value of type U256?

 fn validate_block (block: Block) {
        // String + U256
        let nonce_hash = block.nonce + block.hash

What do you mean?

I'm trying to add (2) values of different types together. A String and a U256 type.

For example:

"000" + 108011115978184024951881204303118891957851514861193131806002042401964770719913

// Output

= 000108011115978184024951881204303118891957851514861193131806002042401964770719913

What should the type of the output be?

What crate is U256 from? That's not a standard type and duckduckgo brings up several possibilities.

1 Like

Are you talking about types which come from one of the RustCrypto crates? In that case, you probably have value of type GenericArray<u8, U256>. This type effectively is [u8; 256], so you need to serialize it first before concatenating to an UTF-8 encoded string. One option is to use the hex crate, another is to use format!("{:x}", block.hash).

I'm using the ethereum_types crate -> (use ethereum_types::U256);

I would like the output to be a value of type U256 like this 108011115978184024951881204303118891957851514861193131806002042401964770719913

So, do you want to do

  • parse the string to a number
  • then add both numbers

Do you want to

  • parse the string to a number
  • compute it log10
  • multiply the other number by 10^(log10 ...)
  • add

If the Output should be a number and not a string, I don't see a Point of leading zeros.

I want to

  • parse the string to a number
  • then add both numbers

Can you use both


(Please note that both links link to different functions)

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.