BCD List to HEX List
John Machin
sjmachin at lexicon.net
Mon Jul 31 17:08:37 EDT 2006
Philippe Martin wrote:
> Paul Rubin wrote:
>
> > Philippe Martin <pmartin at snakecard.com> writes:
> >> I actually need numbers much larger than 32 bits.
***NOW*** you tell us, after all the stuffing about re 32 bits.
> >
> > What is the max size hex number you need? What is the application if
> > you don't mind my asking?
>
> Well I am under NDA so I cannot tell you what the application is - I need
> numbers (dec) with up to 24 digits.
So why don't you get a freely available "bignum" package, throw away
the bits you don' t want, and just compile it and use it, instead of
writing your own bug-ridden (see below) routines? Oh yeah, the bignum
package might use "long" and you think that you don't have access to
32-bit longs in the C compiler for the 8-bit device that you mistook
for an arm but then said is an Smc8831 [Google can't find it] with a
CPU that you think is a SC88 [but the manual whose URL you gave is for
an S1C88] ...
>
> As I said, I went the other way - more data on the line (from dev 1 to dev
> 2) - but I feel there is no speed issue at this stage.
>
> Seems to work.
Nothing is what it seems. See below.
>
>
> Regards,
>
> Philippe
>
>
>
> **** PYTHON ******
> l2 = [1,2,3,4]
> l1 = [0,2,5,9]
>
>
> def sup (l1, l2): #assume same length
> for i in range(len(l1) ):
> if l1[i] > l2[i]:
> return 1
> if l1[i] < l2[i]:
> return -1
> return 0
>
>
>
> def add (l1, l2): #assume same length
> r = []
> idx = range (len(l1))
> idx.reverse()
> carry = 0
> for i in idx:
>
> if l1[i] + l2[i] > 10:
**** SHOULD BE >=
currently add([6, 6], [4, 4] -> [10, 10]
> carry = 1
> r.insert(0,(l1[i] + l2[i]) % 10)
*** try - 10 instead of % 10
If the first operand is > 19, you have a bug!
This might save a few CPU cycles on your smartcard
> else:
> r.insert(0,l1[i] + l2[i] + carry)
> carry = 0
**** SHOULD CHECK FOR CARRY AT END
currently add([9], [8]) -> [7]
should return [1, 7] or handle overflow somehow
> return r
>
>
>
> def sub (l1,l2): #assume same length - sub l1 from l2
*** WHAT HAPPENS IF arg1 > arg2?
> r = []
> idx = range (len(l1))
> idx.reverse()
> carry = 0
> for i in idx:
> print l1[i] + carry, l2[i]
> if ((l2[i]) - (l1[i]+carry) < 0) :
> print 'CARRY'
> r.insert(0,(((10 + l2[i]) - (l1[i]+carry))))
> carry = 1
> else:
> r.insert(0,(l2[i]) - (l1[i]+ carry))
> carry = 0
> return r
>
>
> print sub (l1,l2)
>
> ***** AND AM JUST TESTING IT IN JAVACARD ******
with the bugs in the Python functions carried forward.
>
> //********************************************************************************
> public byte CmpD(byte[] p_op1, byte[] p_op2, byte p_len) {
> byte l_count = (byte)0;
> for (; l_count < p_len; l_count += 1) {
> short C = (short)(p_op1[l_count]);
> short D = (short)(p_op2[l_count]);
>
> if (C > D) return 1;
> if (C < D) return -1;
> }
>
> return 0;
>
> }
> //********************************************************************************
> public static void SubD(byte[] p_op1, byte[] p_op2, byte[] p_dest, byte
> p_len) {
> byte l_count = (byte)0;
> byte l_carry = (byte)0;
> for (l_count = (byte)(p_len - (byte)1); l_count >= (byte)0; l_count
> -= (byte)1) {
> if ((p_op2[l_count] - (byte)(p_op1[l_count]+l_carry) ) < 0) {
> p_dest[l_count] = (byte)( ((byte)10 + p_op2[l_count]) -
> (byte)(p_op1[l_count] + l_carry)) ;
> l_carry = (byte)1;
> }
> else {
> p_dest[l_count] = (byte)( p_op2[l_count] - (byte)(p_op
> [l_count] + l_carry)) ;
> l_carry = -(byte)0;
**** MINUS ZERO?
> }
> }
>
> }
> //********************************************************************************
> public static void AddD(byte[] p_op1, byte[] p_op2, byte[] p_dest, byte
> p_len) {
> byte l_count = (byte)0;
> byte l_carry = (byte)0;
> for (l_count = (byte)(p_len - (byte)1); l_count >= (byte)0; l_count
> -= (byte)1) {
> if (p_op2[l_count] + (byte)(p_op1[l_count]) > 10) {
> p_dest[l_count] = (byte)( ( p_op2[l_count] + p_op
> [l_count] )% 10) ;
> l_carry = (byte)1;
> }
> else {
> p_dest[l_count] = (byte)( p_op2[l_count] + p_op1[l_count] +
> l_carry) ;
> l_carry = -(byte)0;
**** MINUS ZERO?
> }
> }
>
> }
More information about the Python-list
mailing list