I am a novice in python and am going through an opensource project called pyOBD by Donour Sizemore for the ELM327(not really sure,could be aimed at more scantool devices).I can make out that the following is a method to convert a hex value to int.But how does it work? Specially the line with eval in it.
def hex_to_int(str):
i = eval("0x" + str, {}, {})
return i
evalruns a string as if it were Python code, and then outputs the result.In this case, it runs something like
0xaf, which is a way of specifying a hexadecimal literal, and outputs the resulting integer. Try typing0xafinto the Python interpreter, and you'll get an integer as the result.evalis not safe to use on untrusted input. For example,could delete a file on your system.
It would be better to use
ast.literal_evalorint:Which are safe and produce the same result.