Using slow string concatenation:
var = ''
for b in string_var:
var += '%02x' % ord(b)
What you have there is a generator expression; it really produces a sequence to pass to the str.join()
method. ''.join()
concatenates the sequence into a string with an empty delimiter.
To use the ''.join()
call you could build a list first:
results = []
for b in string_var:
results.append('%02x' % ord(b))
var = ''.join(results)
or use a generator function to do the same:
def to_hex(lst):
for b in lst:
yield '%02x' % ord(b)
var = ''.join(to_hex(string_var))