[Tutor] Convert string to long
Joel Goldstick
joel.goldstick at gmail.com
Thu Feb 24 14:32:37 CET 2011
On Thu, Feb 24, 2011 at 8:08 AM, Walter Prins <wprins at gmail.com> wrote:
>
>
> 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
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
Take a look at this code. You get your hex number as a string.
It has 0x on the left which shows its hexidecimal. Get rid of it with the
slice (h[2:] in my example)
Now, use the zfill method on the string to pad the result to 5 characters.
You can pad to any size you want.
Then add back the 0x prefix
q.e.d.
>>> h = hex(546)
>>> h
'0x222'
>>> n = h[2:]
>>> n
'222'
>>> n.zfill(5)
'00222'
>>> '0x' + n.zfill(5)
'0x00222'
>>>
This can all be simplified (well .. shortened!) to
>>> '0x' + hex(543)[2:].zfill(5)
'0x0021f'
--
Joel Goldstick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110224/55aed371/attachment-0001.html>
More information about the Tutor
mailing list