Python Forum

Full Version: Hex input
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

I am looking to have user input 6 hex bytes that are then stored in a variable that I can then send out serial or IP socket, I am fine with the sending data to serial or IP socket, the bit I am struggling with is the entering the hex and storing as hex ????

Many Thanks
You can enter and store the hex as a string.
"hex" is normally just a form for input and output, not storage. For many network protocols you'll send octets in a specified order.

import sys
h = input("Input your hex data ")
try:
    d = int(h, 16)
except ValueError:
    print(f"{h} not a valid hex input")
    sys.exit()
print(f"The entered string {h} is {d} in decimal.")
# Validate d is in the correct range
# Send the data as necessary...