Move utilities from py3compat to utils
[pyelftools.git] / elftools / common / utils.py
index eb51d959b5d3340c0985580a29aedb0f8c9285e1..97c7a039d27362d49f337c8a49f41f1817fccbea 100644 (file)
@@ -121,6 +121,19 @@ def save_dwarf_section(section, filename):
         file.write(data)
     stream.seek(pos, os.SEEK_SET)
 
+def iterbytes(b):
+    """Return an iterator over the elements of a bytes object.
+
+    For example, for b'abc' yields b'a', b'b' and then b'c'.
+    """
+    for i in range(len(b)):
+        yield b[i:i+1]
+
+def bytes2hex(b, sep=''):
+    if not sep:
+        return b.hex()
+    return sep.join(map('{:02x}'.format, b))
+
 #------------------------- PRIVATE -------------------------
 
 def _assert_with_exception(cond, msg, exception_type):