Processing BCD and Signed Integer Fields with Python
Carey Evans
c.evans at clear.net.nz
Wed Apr 12 06:31:18 EDT 2000
"Chris Lada" <chris.lada at westgroup.com> writes:
> Being a newbie in python, I have to ask these questions: what functions can
> I use to process these fields ? What can I use to separate the number from
> the sign ? Is there a conversion for this type of data to strings ? What
> data types would they be ?
Here's some code that works with the kind of data I can get from our
IBM AS/400. It could probably be more efficient, and your mainframe
might not be generating the same data formats, but it should give you
the right idea.
For example:
>>> packed2num('\x12\x34\x5f')
12345L
>>> zoned2num('\xf1\xf2\xd3')
-123L
--------------------
import array
def zoned2num(z):
a = array.array('B', z)
v = 0L
for i in a:
v = (v * 10) + (i & 0xf)
if (a[-1] & 0xf0) == 0xd0:
v = -v
return v
def packed2num(p):
a = array.array('B', p)
v = 0L
for i in a[:-1]:
v = (v * 100) + (((i & 0xf0) >> 4) * 10) + (i & 0xf)
i = a[-1]
v = (v * 10) + ((i & 0xf0) >> 4)
if (i & 0xf) == 0xd:
v = -v
return v
--------------------
--
Carey Evans http://home.clear.net.nz/pages/c.evans/
"Validate me! Give me eternal digital life! Quote me in your .sigs!"
- djc in asr
More information about the Python-list
mailing list