Skip to content

Commit

Permalink
More methods for the BigNumber class
Browse files Browse the repository at this point in the history
  • Loading branch information
benallard committed Sep 22, 2011
1 parent dab58bc commit a675f16
Showing 1 changed file with 43 additions and 6 deletions.
49 changes: 43 additions & 6 deletions pythoncardx/framework/math.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from python.lang import NullPointerException, ArithmeticException
from pythoncard.framework import Util

def from_bcd(data):
"""
Expand All @@ -7,28 +8,36 @@ def from_bcd(data):
"""
return int(hex(data)[2:])

def to_bcd(data):
"""
>>> to_bcd(59)
0x59
"""
a = data // 10
b = data % 10
return (a << 4) + b

def from_hex(data):
"""
>>> from_hex(0x59)
786
"""
return data
to_hex = from_hex

class BigNumber(object):
FORMAT_BCD = 1
FORMAT_HEX = 2

def __init__(self, maxBytes):
if maxBytes <= 0:
def __init__(self, maxBytes = None):
if maxBytes is not None and maxBytes <= 0:
raise ArithmeticException()
self.maxBytes = maxBytes
self._value = None

def init(self, bArray, bOff, bLen, arrayFormat):
if bLen == 0:
raise ArithmeticException()
if arrayFormat not in (self.FORMAT_BCD, self.FORMAT_HEX):
raise ArithmeticException()
if bArray is None:
raise NullPointerException()
if arrayFormat == self.FORMAT_BCD:
Expand All @@ -40,22 +49,50 @@ def init(self, bArray, bOff, bLen, arrayFormat):
self._value = 0
for i in xrange(bLen):
self._value = self._value << 8
self._value += bArray[bOff+i]
self._value += from_hex(bArray[bOff+i])
else:
raise ArithmeticException

@staticmethod
def getMaxBytesSupported():
# We have to lie there for practical reasons
return 20

def multiply(self, bArray, bOff, bLen, arrayFormat):
other = BigNumber(70)
other = BigNumber()
other.init(bArray, bOff, bLen, arrayFormat)
self._value *= other._value

def subtract(self, bArray, bOff, bLen, arrayFormat):
other = BigNumber()
other.init(bArray, bOff, bLen, arrayFormat)
self._value -= other._value

def compareTo(self, operand):
if self._value == operand._value:
return 0
elif self._value > operand._value:
return 1
else:
return -1

def toBytes(self, outBuf, bOff, numBytes, arrayFormat):
array = []
if arrayFormat == self.FORMAT_BCD:
value = self._value
while value > 100:
array.append(to_bcd(value % 100))
value = value // 100
elif arrayFormat == self.FORMAT_HEX:
value = self._value
while value > 0xff:
array.append(to_hex(value % 0xff))
value = value // 0xff
else:
raise ArithmeticException
if numBytes < len(array):
raise ArithmeticException
array.extend([0 for i in range(numBytes - len(array))])
array.reverse()
Util.arrayCopy(array, 0, outBuf, bOff, numBytes)

0 comments on commit a675f16

Please sign in to comment.