perl hex() analog in python how to?
I have next perl code:
my $Lon = substr($Hexline,16,8);
say $output_f "Lon: " . hex($Lon) . "";
where $Hexline has “6a48f82d8e828ce82b82…” format
I try it on python
Lon = int(Hexline[16:24], 16)
f.write('lon = %s' % str(Lon)+'\n')
is it right?
EDIT: in perl’s case hex() gives me a decimal value.
Yes, to convert an hexadecimal string to an integer you use
int(hex_str, 16).Note that in your
writemethod call:%dinstead of%s.strto transform the integer into a string.Hence, the write call could be written as:
Alternatively, you could also use
formatthis way: