[Tutor] Convert string to long

Andre Engels andreengels at gmail.com
Thu Feb 24 15:20:46 CET 2011


On Thu, Feb 24, 2011 at 3:03 PM, tee chwee liong <tcl76 at hotmail.com> wrote:
>>>> '0x' + hex(543)[2:].zfill(5)
> '0x0021f'
>
> this is a good way but it's still in string format. but if i convert it to
> long, then the leading 0s will be truncated. i guess can't have it both way.

A long is just a number. You cannot say that a number has or does not
have leading zeroes. Only _a representation of_ that number has. The
numbers 3, 1+2 and 0000000000003 are all the same number, so you
cannot say that the first does not have leading zeroes whereas the
last one has. To make the concept of 'leading zeroes' a meaningful
one, you _first_  have to re-convert the number to a string. Whether
or not there are leading zeroes depends on how that conversion is
done. If you use Python's standard conversion method, the result will
be a string representation without leading zeroes, but there are other
conversion methods that do give leading zeroes.

-- 
André Engels, andreengels at gmail.com


More information about the Tutor mailing list