actionscript 3.0 cookbook:Int64与Number转换

来源:互联网 发布:快递如何找淘宝客户 编辑:程序博客网 时间:2024/05/16 02:01
package com.netease.protobuf {
        public final class Int64 extends Binary64 {
                public final function set high(value:int):void {
                        internalHigh = value
                }
                public final function get high():int {
                        return internalHigh
                }
                public function Int64(low:uint = 0, high:int = 0) {
                        super(low, high)
                }
                /**
                 * Convert from <code>Number</code>.
                 */
                public static function fromNumber(n: Number):Int64 {
                        return new Int64(n, Math.floor(n / 4294967296.0))
                }
                /**
                 * Convert to <code>Number</code>.
                 */
                public final function toNumber():Number {
                        return high * 4294967296.0 + low
                }
                public final function toString(radix:uint = 10):String {
                        if (radix < 2 || radix > 36) {
                                throw new ArgumentError
                        }
                        switch (high) {
                                case 0:
                                {
                                        return low.toString(radix)
                                }


                                case -1:
                                {
                                        if ((low & 0x80000000) == 0)
                                        {
                                                return (int(low | 0x80000000) - 2147483648.0).toString(radix)
                                        }
                                        else
                                        {
                                                return int(low).toString(radix)
                                        }
                                }


                                default:
                                {
                                        break;
                                }
                        }
                        if (low == 0 && high == 0) {
                                return "0"
                        }
                        const digitChars:Array = [];
                        const copyOfThis:UInt64 = new UInt64(low, high);
                        if (high < 0) {
                                copyOfThis.bitwiseNot()
                                copyOfThis.add(1)
                        }
                        do {
                                const digit:uint = copyOfThis.div(radix);
                                if (digit < 10) {
                                        digitChars.push(digit + CHAR_CODE_0);
                                } else {
                                        digitChars.push(digit - 10 + CHAR_CODE_A);
                                }
                        } while (copyOfThis.high != 0)
                        if (high < 0) {
                                return '-' + copyOfThis.low.toString(radix) +
                                                String.fromCharCode.apply(
                                                String, digitChars.reverse())
                        } else {
                                return copyOfThis.low.toString(radix) +
                                                String.fromCharCode.apply(
                                                String, digitChars.reverse())
                        }
                }
                public static function parseInt64(str:String, radix:uint = 0):Int64 {
                        const negative:Boolean = str.search(/^\-/) == 0
                        var i:uint = negative ? 1 : 0
                        if (radix == 0) {
                                if (str.search(/^\-?0x/) == 0) {
                                        radix = 16
                                        i += 2
                                } else {
                                        radix = 10
                                }
                        }
                        if (radix < 2 || radix > 36) {
                                throw new ArgumentError
                        }
                        str = str.toLowerCase()
                        const result:Int64 = new Int64
                        for (; i < str.length; i++) {
                                var digit:uint = str.charCodeAt(i)
                                if (digit >= CHAR_CODE_0 && digit <= CHAR_CODE_9) {
                                        digit -= CHAR_CODE_0
                                } else if (digit >= CHAR_CODE_A && digit <= CHAR_CODE_Z) {
                                        digit -= CHAR_CODE_A
                                        digit += 10
                                } else {
                                        throw new ArgumentError
                                }
                                if (digit >= radix) {
                                        throw new ArgumentError
                                }
                                result.mul(radix)
                                result.add(digit)
                        }
                        if (negative) {
                                result.bitwiseNot()
                                result.add(1)
                        }
                        return result
                }
        }

}

更多关于actionscript 3.0 cookbook的知识可以查询天地会http://bbs.9ria.com/thread-25133-1-1.html

0 0
原创粉丝点击