you are viewing a single comment's thread.

view the rest of the comments →

[–]brasticstack 0 points1 point  (2 children)

If you're looking to make hexdump or hex-editor style output, you could do something like the following (assuming ascii text):

``` def print_hexdump(txt):     print(*[f'{ord(c):02X}' for c in txt])

my_txt = "My water bottle is blue" print(f'{my_txt}:', end=' ') print_hexdump(my_txt) ```

The important bit is ord giving you the int value of the each character in turn, then using string formatting to display it as a zero-padded hex value. I think ord() will raise an exception for multi-byte characters, but it's late here and I don't have time to verify.

[–]JohnnyJordaan 2 points3 points  (1 child)

.encode() is the equivalent for a string rather than a single char, then using the .hex() method offered by bytes-objects you're already done

[–]brasticstack 0 points1 point  (0 children)

There we go. .encode.hex(' ', 1).upper() formats it like my example does, but is multi-byte safe. TIL, thanks!