1

Given a bytes object and an index, I want to read a string from the bytes between the index and the next null(\x00). For example:

bstr=b'abc\x00defg'
addr_to_str(bstr,0) # 'abc'
addr_to_str(bstr,1) # 'bc'
addr_to_str(bstr,4) # 'defg'

I can do the following:

def addr_to_str(bstr,i):
    return bstr[i:].split(b'\x00')[0].decode('utf-8')

Since the bytes object is very large, I would like an efficient version of addr_to_str. I am new to Python and not knowing how the interpreter treats the split() call when I only want to access the first split result. Or is the .decode() necessary?

Question: Is there a better and pythonic way to do addr_to_str()?

1

1 Answer 1

1

How about (in Python 2):

def addr_to_str(s, pos):
   end = s.find('\0', pos + 1)
   if end != -1:
      return s[pos:end]
   else:
      return s[pos:]

This scans the string once looking for a null, and then creates a slice.

Sign up to request clarification or add additional context in comments.

4 Comments

s is a bytes not a string. It complains s.find('\0',). The function should take a bytes and return a string.
Works for me in Python 2.7. If you're using Python 3, you might want to specify that.
So, in Python 3, seem that the .decode() needs to be explicitly called.
In python 3, for bytes, use s.find(b'\0', pos + 1)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.