BCD List to HEX List
Philippe Martin
pmartin at snakecard.com
Mon Jul 31 10:52:23 EDT 2006
Paul Rubin wrote:
> Philippe Martin <pmartin at snakecard.com> writes:
>> I actually need numbers much larger than 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.
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.
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:
carry = 1
r.insert(0,(l1[i] + l2[i]) % 10)
else:
r.insert(0,l1[i] + l2[i] + carry)
carry = 0
return r
def sub (l1,l2): #assume same length - sub l1 from l2
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 ******
//********************************************************************************
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;
}
}
}
//********************************************************************************
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;
}
}
}
More information about the Python-list
mailing list