简体   繁体   中英

Get an UTF-16 string length from memory in python

I need to read a utf-16 encoded string that is stored in memory in a python script for LLDB. According to their documentation I'm able to use ReadMemory(address, length, error) but I need to know its length in advance. If not python's decode function fails when it stumbles upon a character it cannot decode (even using the 'ignore' option) and the process stops:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u018e' in position 12: ordinal not in range(128)

Can anyone suggest a way of achieving this? (either using a "python" or "lldb python" implementation). I don't have the original string's length.

Thanks.

Is the string 0-terminated? If so, you could read 2 bytes at a time, until you encounter 0x0000, and then you'd know you have a complete string.

If you do this, you'd want to give yourself a constraint (eg "I will give up after reading - say - 1MB of data", in case you're running into corrupted memory).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM