[Numpy-discussion] Numeric odd tostring() behaviour

Todd Miller jmiller at stsci.edu
Tue Oct 21 09:59:28 EDT 2003


I think the problem is that the default integer precision is most likely
32-bits, and you appear to be assuming it will be 8-bits.  If you
declare your array using typecode=Numeric.UInt8 as an extra parameter,
you will force the type to match your assumption and things will work
out as you expect.

Regards,
Todd

On Tue, 2003-10-21 at 00:28, Russell Valentine wrote:
> Hello,
> 
>    It may be my fault, but I think the following behaviour is odd. If I
> try to change a array to a string it seems like it adds a lot of extra
> zero characters. Take the following script attached as a example, it gives
> me this output.
> 
> ta.tostring is not equal
> Zero characters - 25
> 255 characters - 3
> tast is equal
> Zero characters - 4
> 255 characters - 3
> 
> tostring() is so much more faster than the second way, but it isn't giving
> me the desired results. Have I done something wrong? I'm using Numeric
> 23.1
> 
> Thanks for your help.
> 
> 
> Russell Valentine
> ----
> 

> #!/bin/env python
> 
> import string
> import Numeric
> 
> 
> ta = Numeric.array([0, 255, 255, 255,0,0,0])
> compare_string = "\x00\xff\xff\xff\x00\x00\x00"
> if ta.tostring() == compare_string:
>     print "ta.tostring is equal"
> else:
>     print "ta.tostring is not equal"
> 
> print "Zero characters - "+str(ta.tostring().count("\x00"))
> print "255 characters - "+str(ta.tostring().count("\xff"))
> 
> tast = ""
> for value in ta:
>     tast += chr(value)
> 
> if tast == compare_string:
>     print "tast is equal"
> else:
>     print "tast is not equal"
> 
> print "Zero characters - "+str(tast.count("\x00"))
> print "255 characters - "+str(tast.count("\xff"))
> 
-- 
Todd Miller 			
Space Telescope Science Institute
3700 San Martin Drive
Baltimore MD, 21030
(410) 338 - 4576





More information about the NumPy-Discussion mailing list