Release the python library absolute32

Source: Internet
Author: User
Tags crc32
Lai Yonghao (http://laiyonghao.com)
Question 1

In a later version of Python, when two int values overflow, it automatically converts the result to the long type. For example:

 
>>> 0x7fffffff + 12147483648l

This feature is good, but it has different results from the C language. If you want to pack the result into a four-byte buffer and send it to another process, the result will be quite tangled:

 
>>> Import struct >>> struct. pack ("I", 0x7fffffff + 1) traceback (most recent call last): file "<stdin>", line 1, in <module> struct. error: Long too large to convert to int

I encountered this problem when designing my webgame network protocol.
Question 2

On Different hardware platforms, the return values of the same function may be different. For example, in Hash (), both 32-bit and 64-bit return int values, but the values vary greatly:

 
>>> Hash ('copyright' * 10) #32-bit platform-942199392 >>> Hash ('copyright' * 10) #64-bit platform-6555514777893392992

Imagine if you write a k/V stored file in 32-bit and put it in 64-bit to read it, or vice versa, will it make you crazy?
Question 3

In different Python versions, the return values of many functions are also different. For example, the CRC32 function in zlib, well, yes, don't think it will be consistent at the end of 32! The following references are from Python manuals:

Changed in version 2.6: the return value is in the range [-2 ** 31, 2 ** 31-1] regardless of platform. in older versions the value is signed on some platforms and unsigned on others.

Changed in version 3.0: the return value is unsigned and in the range [0, 2 ** 32-1] regardless of platform.

See it, zlib. CRC32 (and zlib. adler32) although it has nothing to do with the 32/64-bit platform, the range of returned values in 3.x and 2.x versions is different. Imagine that the network protocol you designed uses CRC/Adler.AlgorithmTo calculate the checksum, which is then used for python 3.x and 2.x.ProgramWill communication go crazy?
Solution

The solution is obviously consistent. Write a substitute for these egg functions to ensure that they return the same value in different hardware and versions.
So I wrote my own version of the ADD, hash, CRC, and Adler functions to ensure that their return values are signed 32-bit integer (that is, the value range is [-2 ** 31, 2 ** 31-1]). After testing Ubuntu 10.04 lts 32-bit/64-bit + Python 2.6/3.1, we used it in our network protocol processing.
Later, I typed it into a lib named absolute32 and threw it to Google Code hosting (http://code.google.com/p/absolute32/). At the same time, I registered it in pypi to make it easy for friends who need it, it is regarded as a good thing.
Installation and Use of absolute32

The installation is very simple, because it has been uploaded to pypi, so simply execute:

 
Easy_install-u absolute32

Just install it. Finally, let's take an example and enjoy it!

 
Import absolute32 as aassert. add (0x7fffffff, 2) =-0x7fffffffassert. hash ('copyright') =-174803930 assert. adler (B 'copyright') = 322503642 assert. CRC (B 'copyright') = 947983859
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.