python类型强制转换long to int的代码

WBOY
Release: 2016-06-16 08:46:49
Original
2356 people have browsed it

python2.4版本以后,如果int的值超出范围不会溢出,而是内部转换为long,在网上没有找到从long型强制转换成int的代码,这里所说的int取值范围是和java里一致,即用四个字节表示。
自己写了一个函数,勉强可以用,供大家参考。

复制代码 代码如下:

import sys
def LongToInt(value):
    assert isinstance(value, (int, long))
    return int(value & sys.maxint)

经过测试,在32位和64位上运算结果一致。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template