468,294 Members | 1,809 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,294 developers. It's quick & easy.

checksum works in unix but not win32 os

i have the following code...which works fine in UNIX but then i move it
over to WINDOWS XP and check the sum on the same file that i tested on
unix and i get different results.

def checksum(fileobj):
filedata = array.array('B', fileobj.read())
totbytes = len(filedata)
result = 0
def rotate_right(c):
if c&1: return (c>>1)|0x8000
else: return c>>1
for ch in filedata:
result = (rotate_right(result)+ch) & 0xFFFF
return result

Unix File = 8683
Win32 = 23662

Please let me know why its acting different in WIN32, also it only does
this on SOME files...out of 50 i tested only 5 it failed on.

Jul 18 '05 #1
2 1416
[GujuBoy wrote]
i have the following code...which works fine in UNIX but then i move it
over to WINDOWS XP and check the sum on the same file that i tested on
unix and i get different results.

def checksum(fileobj):
filedata = array.array('B', fileobj.read())
totbytes = len(filedata)
result = 0
def rotate_right(c):
if c&1: return (c>>1)|0x8000
else: return c>>1
for ch in filedata:
result = (rotate_right(result)+ch) & 0xFFFF
return result

Unix File = 8683
Win32 = 23662

Please let me know why its acting different in WIN32, also it only does
this on SOME files...out of 50 i tested only 5 it failed on.


By "failed" you mean the checksum is different? The file itself (if it
is a text file) might very well have different content: EOL characters
are different on Windows (CRLF) and Unix (LF). If a text file is opened
in textmode (the default), then the CRLF's will get translated to LF's,
however, if opened in binary mode (by having a 'b' in the mode option to
open()/file()) then this translation is not done.

Also, any reason you are not just using the hexdigest() method in the
md5 module?

Cheers,
Trent

--
Trent Mick
Tr****@ActiveState.com
Jul 18 '05 #2
GujuBoy wrote:
i have the following code...which works fine in UNIX but then i move it
over to WINDOWS XP and check the sum on the same file that i tested on
unix and i get different results.

def checksum(fileobj):
filedata = array.array('B', fileobj.read())


Did you make sure you opened this "fileobj" using
"rb" instead of the default "r"? Look for the
open() call that created it and put "rb" in... otherwise
CRLF combinations are converted to LF on input...

-Peter
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Peter Abel | last post: by
2 posts views Thread by GujuBoy | last post: by
6 posts views Thread by Matthew | last post: by
7 posts views Thread by sea | last post: by
2 posts views Thread by pradeep | last post: by
6 posts views Thread by Astroman | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.