pythonhexbytememoryview

Input string at a specific offset in memoryview


I would like to modify some values inside a memoryview in Python at a specific offset. Example:

str = "Ys"
#let us assume memoryview[i:i+2].to_bytes() contains b'\x00\x01'
memoryview[i:i+2] = some_function(str)
print(memoryview[i:i+2].to_bytes()) #now contains b'\x59\x73'

How can I do it? I tried everything but I cannot make it work. Thanks in advance.


Solution

  • It is enough to write

    bytes(str, 'ascii) #or different encode
    

    I could not understand it because I was trying to put ASCII characters over an ASCII symbol, for instance an 's' over a '\x00' byte which is not printable, then maybe the output sometime can be represented as a byte or as a character, but is still put in the correct way