binnum.py :  » Business-Application » ThanCad » thancad-0.0.9 » p_gdbfpy » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Business Application » ThanCad 
ThanCad » thancad 0.0.9 » p_gdbfpy » binnum.py
'''\
functions for binary encoding/decoding of numeric values
mocons.lib.utils.binnum.py
jjk  02/03/98  001  from mdcutil.py import 
jjk  02/13/98  002  add unsigned 2,4 from Intel import 
jjk  02/19/98  003  add unsigned as Intel 2,4 
jjk  11/15/99  004  documentation updates

Equivalent built-in python functions may be available.
If so, I wrote these before they were available, or
before I was aware of them. :-)

def integers_from_char1(int_string):
def integer_from_char1(int_string):
def integers_from_Intel2(int_string):
def integer_from_Intel(int_string):
def integer_from_Intel2(int_string):
def integers_from_Intel4(int_string):
def integer_from_Intel4(int_string):
def unsigned_from_Intel2(int_string):
def unsigned_from_Intel4(int_string):
def unsigned_as_Intel2(uint):
def unsigned_as_Intel4(uint):

*** !!  USE AT YOUR OWN RISK    !! ***
*** !! NO WARRANTIES WHATSOEVER !! ***

Jeff Kunce <kuncej@mail.conservation.state.mo.us>
'''

def integers_from_char1(int_string):
  # return list of integers decoded from 1-byte-format string
  # jjk  12/08/95
        lst = []
  pos = 0
  while (pos < len(int_string)):
    lst.append(integer_from_char1(int_string[pos]))
    pos = pos + 1
  return(lst)

def integer_from_char1(int_string):
  # return first integer decoded from 1-byte-format string
  # jjk  12/08/95
  return(ord(int_string))

def integers_from_Intel2(int_string):
  # return list of integers decoded from 2-byte-intel-format string
  # jjk  12/08/95
        lst = []
  pos = 2
  while (pos <= len(int_string)):
    lst.append(integer_from_Intel2(int_string[pos-2:pos]))
    pos = pos + 2
  return(lst)

def integer_from_Intel(int_string):
  # return signed integer decoded from n-byte-intel-format string
  # jjk  04/25/96  handles negative #'s
    value = 0
    isNegative = 0
    if (len(int_string) > 0):
      bytes = map(ord,int_string)
      bytes.reverse()
      if (bytes[0] >= 0x80):
        isNegative = 1
        bytes[0] = bytes[0] - 0x80
      for byte in bytes:
        value = (value * 256) + byte
    if isNegative:
      value = -value
    return value

def integer_from_Intel2(int_string):
  # return first integer decoded from 2-byte-intel-format string
  # jjk  04/25/96  
    return integer_from_Intel(int_string[:2])

def integers_from_Intel4(int_string):
  # return list of integers decoded from 4-byte-intel-format string
  # jjk  01/11/96
    lst = []
    pos = 0
    while (pos <= len(int_string)):
      lst.append(integer_from_Intel4(int_string[pos:(pos+4)]))
      pos = pos + 4
    return(lst)

def integer_from_Intel4(int_string):
  # return first integer decoded from 4-byte-intel-format string
  # jjk  04/25/96
    return integer_from_Intel(int_string[:4])

def unsigned_from_Intel2(int_string):
  '''return unsigned signed integer decoded from byteintelformatstring import 
  jjk 11/05/97  from mdcutil.py import 
  return ord(int_string[0]) + (256 * ord(int_string[1]))

def unsigned_from_Intel4(int_string):
  '''return unsigned signed integer decoded from byteintelformatstring import 
  jjk 11/05/97  from mdcutil.py import 
  return (unsigned_from_Intel2(int_string[:2]) + 
    (65536 * unsigned_from_Intel2(int_string[2:])))

def unsigned_as_Intel2(uint):
  '''return 2-byte-intel-format string encoded from unsignedsignedinteger import 
  jjk  02/19/98'''
  encInt = int(abs(uint))  #enforce unsingned integer
  hiByte = encInt/256
  loByte = encInt%256
  return(chr(loByte)+chr(hiByte))

def unsigned_as_Intel4(uint):
  '''return 4-byte-intel-format string encoded from unsignedsignedinteger import 
  jjk  02/19/98'''
  encInt = int(abs(uint))  #enforce unsingned integer
  hiWord = encInt/65536
  loWord = encInt%65536
  return(unsigned_as_Intel2(loWord)+unsigned_as_Intel2(hiWord))
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.