Question

I want to retrieve hexadecimal data from user, using python. How to retrieve the data from user and convert it to hex.

 #to read varibales  from Python

STX        =  '\xF7' #hex(input("enter STX Value"))
Deviceid   =   hex(input("enter device id"))
subid      =   hex(input("enter address of the Device and load details"))
Comnd      =   hex(41)
Data       =   hex(01)
EorCode    =   input("enter EOR Code")
ADD_sum    =   '\xF2' #hex(input("Enter Add sum value"))

tuple  = (STX, Deviceid,subid,Comnd,Data,EorCode,ADD_sum)
print tuple

i am reading the above data from user,but i am getting output as follows

enter device id03
enter address of the Device and load details81
enter EOR Code32
('\xf7', '0x3', '0x51', '0x29', '0x1', '0x20', '\xf2')

But i need to be printed as 0x03 and 0x01.

I am very new to PYTHON please help.

Was it helpful?

Solution

You're looking for string formatting:

>>> "0x{0:04x}".format(42)
'0x002a'

So you'll want to modify your lines like so:

Deviceid   =   "0x{0:2x}".format((input("enter device id"))

Also, if any other Python developer will be looking at this code you may want to look at the Python style guide, PEP8.

Following the style guide, your code might look like this:

stx = '\xF7'  # hex(input("enter STX Value"))
device_id = hex(input("enter device id"))  # deviceid might also be fine
sub_id = hex(input("enter address of the Device and load details"))
comnd = hex(41)
data = hex(01)
eor_code = input("enter EOR Code")
add_sum = '\xF2'  # hex(input("Enter Add sum value"))

values  = (stx, device_id, sub_id, comnd, data, eor_code, add_sum)
print values  # tuple is a keyword - it's best to *not* override them if possible

Of course,

A style guide is about consistency. Consistency with this style guide is important. Consistency within a project is more important. Consistency within one module or function is most important.

But most importantly: know when to be inconsistent -- sometimes the style guide just doesn't apply. When in doubt, use your best judgment. Look at other examples and decide what looks best. And don't hesitate to ask!

OTHER TIPS

It seems to me that all you really need is to specify how to print the numbers, but hex function returns a string. Because in python, '10' is a string and this is different from 10, which is an int. Python is dynamicaly, but strongly typed language.

So in order to have output you want, you may choose from 2 options:

  1. write your own function to convert numbers to hexaxecimal numbers in a format you want and use it instead of hex:

    def myhex(num):
        return '0x%02x' % num
    

    this 0x%02x means - first, 0x is just normal text which you probably want to prefix all your hexadecimal numbers, %02x means: print argument as hexadecimal number of length 2, prefixed with 0 if it's too short (one-digit hexadecimal number).

  2. do not convert numbers to hex when reading values (it's probably a good thing to work with numbers represented as numbers) and print them formated to your specification at the end:

    print '(' + ', '.join('%0x02x' % x for x in tuple) + ')'
    

    which creates list of all values in tuple (btw, avoid using keywords as your variable names when possible) converted to correct 2-digit hexadecimal numbers with 0x prefixes, joins them using ', ' and surrounds them with parentheses. But feel free to change it - I'm just building on your example and trying to duplicate your output.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top