- hex_value = ''.join('0x{} '.format(c.encode('hex')) for c in ascii_string).strip()
- if padding and (len(ascii_string) < length):
- hex_value += ''.join(' 0x00' for _ in range(len(ascii_string), length))
-
- return hex_value
+ if padding:
+ ascii_string = ascii_string.ljust(length, '\0')
+ return ' '.join('0x{}'.format(c.encode('hex')) for c in ascii_string)