0

Hi Guys ,
How can i change the 5th and 6th byte from this bytes8 0x3FcB875f"0000"ddC4,or just arrive at this solution where:
last four bytes(of bytes8) are this way 0x********"0000ddC4" and the first four bytes(of bytes8) can have any number but !=0 ?

bytes8 normal=0x3FcB875f56beddC4
// POSSIBLE SOLUTION //
bytes8 conver=0x3FcB875f0000ddC4
bytes8 anothe=0x****0000ddC4

Where 1 ore more of * is != 0


bytes8 another example solution=*0x000100100000ddC4*
bytes8 another example solution=*0x00a10f000000ddC4*

This is what i try:

bytes8 txOriginBytes8= 0x3FcB875f56beddC4;// original 8bytes 0x3FcB875f56beddC4

bytes2 twoBytes= bytes2(uint16(uint64(txOriginBytes8)));// take last two bytes: 0xddC4

bytes4 fourBytes=twoBytes;
// add to a four bytes array: 0xddC40000

bytes4 shiftTwoBytes=fourBytes >> 16;// shifting right by two bytes 0x0000ddC4

bytes8 toEithBytes=bytes8(uint64(uint32(shiftTwoBytes)));// put in a bytes8 array 0x000000000000ddc4

return toEithBytes;

1 Answer 1

1

I suppose the easiest way is to convert it to bytes, make your modifications and convert it back again like so :

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.5;

contract Example {

  function changeByte8() public view returns (bytes8) {
    bytes8 value = 0x3FcB875f56beddC4;

    // encodePacked the value to get a bytes array
    bytes memory array = abi.encodePacked(value);

    // Set 5th and 6th bytes to 0
    array[4] = 0x00;
    array[5] = 0x00;

    // Ensure that bytes 1 to 4 are != 0
    array[0] = 0xFF;
    array[1] = 0xFF;
    array[2] = 0xFF;
    array[3] = 0xFF; 

    // convert back to bytes8
    return bytes8(array); // 0xffffffff0000ddc4
  }
}

Tell me if you are looking more for an approach based on bit operators and I'll edit my answer.

EDIT : Well actually it's just a few lines, so I add it here too. (I wrote it for clarity, you can be more concise by generating the masks with at the moment they are used) :

 function changeByte8bits() public view returns (bytes8) {
    bytes8 value = 0x3FcB875f56beddC4;

    // Bit mask to select bytes 1 to 4
    bytes8 mask14 = 0xFFFFFFFF00000000;

    // Bit mask to select bytes 5 to 6
    bytes8 mask56 = 0x00000000FFFF0000;

    // Ensure that all bits in bytes 1 to 4 are set
    value = value | mask14;

    // Ensure that all bits in bytes 5 to 6 are unset
    value = value & ~mask56;

    return value; // 0xffffffff0000ddc4
  }
6
  • Ohhhhh gooshh !! Thanks You Sooo Much !!! <3 I spent so much time on that !!! My miss was the fact that i didnt now that i can convert my bytes8 to an memory array with abi.encode() ! "And Because on fixed bytes array ,the single byte can be read only, and cannot be set " However Thanks a Lot ! Love your solution so simple and perfect ! Commented Mar 17, 2022 at 13:21
  • Just one Thing Do You Know How To Fix This ? I use your first function and have solidity 0.8.0 compiler . ERROR: type conversion not allowed from "bytes memory" to "bytes8". --> somenewStuf.sol:63:12: | 63 | return bytes8(array); Commented Mar 17, 2022 at 13:53
  • 1
    Ah yes sorry that conversion is only available at solidity 0.8.5 and above (blog.soliditylang.org/2021/06/10/…) my bad. Tell me if you cannot use it I can edit my answer.
    – hroussille
    Commented Mar 17, 2022 at 13:58
  • No its Fine 0.8.5 solidity , Nr 1 ! Commented Mar 17, 2022 at 14:02
  • Alright, I edited the pragma to avoid any confusion. Sorry for the mistake.
    – hroussille
    Commented Mar 17, 2022 at 14:04

Your Answer

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

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