[Tutor] Convert string to long

Walter Prins wprins at gmail.com
Thu Feb 24 14:08:37 CET 2011


On 24 February 2011 11:50, tee chwee liong <tcl76 at hotmail.com> wrote:

>  > int(s,16) for a hex string
> >
>
> great but the leading zeroes are being truncated.
>

You need to seperate the concept of display/formatting of some thing from
the actual thing/value being displayed.

Normally when we humans communicate numbers and or work with them, leaading
zero's are not used, so normally most computer systems and languages will
not display numbers with leading zeros by default.  It is therefore up to
you to *tell* the computer you want leading zeros in order for it to produce
them from the actual value being represented.

Furthermore you need to distinguish (as does the computer) between different
object types (namely strings and numbers) as they are different animals
which are handled differently by the computer.

A number, as already mentioned, will be by default not displayed with
leading zeros as that's normally how humans are used to seeing numbers.

A string however is a data structure that can contain arbitrary characters.
The computer therefore will generally just display a string with whatever is
in it (some exceptions apply for escape characters etc depending on context
etc. but ignore that for the moment.)

Now, a string may contain characters that happens to be the character
representation of number (with or without leading zeros) but yet to the
computer this remains a string and only a string, until you *explicitly*
tell it otherwise and explicitly convert it into an actual number object.
After you've done this of course, the computer will know that the thing now
being dealt with is in fact a number, and will therefore display/format the
number as it usually does (e.g. without leading zeros), again, unless you
tell it to display/format it otherwise.

So at the risk of belaboring the points: 1) Get a handle on the fact that
numbers and strings are different things, and that on the one hand you're
converting between them.  2) Get a handle on the fact that different things
can furthermore be displayed/formatted in a variety of different ways, and
there may be many ways to display or represent a given thing, which again is
up to *you* to control/specify.

Walter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110224/13372286/attachment.html>


More information about the Tutor mailing list