[Tutor] Ascii hex to binary

cino hilliard hillcino368@hotmail.com
Tue Aug 5 10:34:04 EDT 2003


This is a multi-part message in MIME format.

------=_NextPart_000_65c4_673f_62d1
Content-Type: text/plain; format=flowed


Hi, jimmy

Thanks for your inquiry.

You may want to try my base converter base  2 - 255  and fantastic number 
compresser.
def base(r1,r2,n) works well in python because of the arbitrary precision 
integers. It can
be converted to other languages that have the ascii set and precision up to 
256 places.
I use your request as an example in the comments of the code.

It is also attached.

Have fun in the facinating world of numbers.

>From: "Jimmy verma" <jim_938@hotmail.com>
>To: tutor@python.org
>Subject: [Tutor] Ascii hex to binary
>Date: Sun, 03 Aug 2003 22:44:53 +0530
>
>Hello *
>
>Can some please tell me how can i express an ASCII hexadecimal string in 
>the binary form with python.
>
>10BF31704FAB5B1F03F9B68B1F39A66521B1841F1481697F8E12B7F7DDD6E3D7248D965B1CD45E2114
>
>I want this string to be expressed in binary form.
>
>Thanks in advance.
>
>waiting for reply.


#               Convert from base A to base B for any base(radix) 2 - 255
#                                      Cino Hilliard
#                                        6/9/2003
# Usage: (>>> assumed) import cvbase. cvbase.b2b(r1,r2,num).
# Note num outside of base 10 must be in quotes. No error checking is 
provided. Eg.,
# cvbase.b2b(2,10,11222) will give a bad result since e is not allowed in 
base 2.

# Examples 1 set a to a value base 16 and convert to base 2 and back to base 
16.
# 
a='10BF31704FAB5B1F03F9B68B1F39A66521B1841F1481697F8E12B7F7DDD6E3D7248D965B1CD45E2114'
# >>> cvbase.b2b(16,2,a)
# length before conversion  82
# 
10000101111110011000101110000010011111010101101011011000111110000001111111001101
# 
10110100010110001111100111001101001100110010100100001101100011000010000011111000
# 
10100100000010110100101111111100011100001001010110111111101111101110111010110111
# 
00011110101110010010010001101100101100101101100011100110101000101111000100001000
# 10100
# length after conversion  325
# 
10BF31704FAB5B1F03F9B68B1F39A66521B1841F1481697F8E12B7F7DDD6E3D7248D965B1CD45E2114
# length after converting back 82

# Example 2 convert a to base 255 and back to base 16,
# >>> cvbase.b2b(16,255,a)
# 
10BF31704FAB5B1F03F9B68B1F39A66521B1841F1481697F8E12B7F7DDD6E3D7248D965B1CD45E2114
# length before conversion 82
# J¦+dR + +?+¦ô;ë++vc v@?8ótx+-¦¬5S+z?+-  %ê   #This lost some in hotmail
# length avter conversion 41
# 
10BF31704FAB5B1F03F9B68B1F39A66521B1841F1481697F8E12B7F7DDD6E3D7248D965B1CD45E2114
# length after converting back 82
# notice the 50% compression of your data from base 16 to base 255?
# notice the 4 times increase of your data from base 16 to base 2?
# Going from base 10 to base 255 gives 41.6% compression.
# Try cvbase.b2b(10,255,((10**1000-1)/9)**2)
# keep in mind, we are compressing numbers, not text. Text is a different 
matter.
# However if you have a billion digits of Pi you want to transmit via modem 
to your
# girl friend, you will cut your phone expense by 58.4% by compressing and 
sending
# algorithm.
#                Have fun in the facinating world of numbers
#

def b2b(r1,r2,n):             #Convert n in base r1 to base r2 and back
    import string
    print n
    print "length before conversion ",len(str(n))
    x = base(r1,r2,n)
    print x
    print "length after conversion ",len(str(x))
    n = base(r2,r1,x)
    print n
    print "length after converting back ",len(str(n))

def base(r1,r2,num):       #This is the crown jewel of the routine where we
    import string          #use base 10 as a pivot to go from base r1 to r2.
    digits=""
    for j in range(0,10):               #We collect the ascii characters we 
want to use.
          digits = digits + chr(j+48)
    for j in range(10,36):
          digits = digits + chr(j+55)
    for j in range(36,52):
          digits = digits + chr(j-4)
    for j in range(52,59):
          digits = digits + chr(j+6)
    for j in range(59,224):
          digits = digits + chr(j+32)
    for j in range(224,256):
          digits = digits + chr(j-224)
    num1 = str(num)
    ln  = len(num1)
    dec = 0
    for j in range(ln):
          asci = string.index(digits,num1[j]) #Get the ascii code of num
          temp   = r1**(ln-j-1) #Get the decending powers of the decimal 
encoded
          dec += asci*temp      #Convert the num in r1 to decimal
    RDX = ""                    #Init the storage string
    j=-1
    dec2=dec
    while dec2:                 #get number of digits in decimal
         dec2 = dec2 / r2
         j = j+1
    while j >= 0:
          pwr = r2**j           #This is just going from base 10 to another 
base.
          Q   = dec // pwr      #converting the  decimal number to r2 which 
in effect
          dec = dec % pwr       #converting num in base r1 to a number in 
base r2.
          RDX = RDX + digits[Q]
#         RDX = RDX + str(Q)+","  #like Maple. Using this will allow any 
base conv.
          j-=1
    return RDX

_________________________________________________________________
The new MSN 8: smart spam protection and 2 months FREE*  
http://join.msn.com/?page=features/junkmail

------=_NextPart_000_65c4_673f_62d1
Content-Type: text/plain; name="cvbase.py"; format=flowed
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="cvbase.py"

#               Convert from base A to base B for any base(radix) 2 - 255
#                                 Cino Hilliard
#                                    6/9/2003
# Usage: (>>> assumed) import cvbase. cvbase.b2b(r1,r2,num).
# Note num outside of base 10 must be in quotes. No error checking is 
provided. Eg.,
# cvbase.b2b(2,10,11222) will give a bad result since e is not allowed in 
base 2.

# Examples 1 set a to a value base 16 and convert to base 2 and back to base 
16.
# 
a='10BF31704FAB5B1F03F9B68B1F39A66521B1841F1481697F8E12B7F7DDD6E3D7248D965B1CD45E2114'
# >>> cvbase.b2b(16,2,a)
# length before conversion  82
# 
10000101111110011000101110000010011111010101101011011000111110000001111111001101
# 
10110100010110001111100111001101001100110010100100001101100011000010000011111000
# 
10100100000010110100101111111100011100001001010110111111101111101110111010110111
# 
00011110101110010010010001101100101100101101100011100110101000101111000100001000
# 10100
# length after conversion  325
# 
10BF31704FAB5B1F03F9B68B1F39A66521B1841F1481697F8E12B7F7DDD6E3D7248D965B1CD45E2114
# length after converting back 82

# Example 2 convert a to base 255 and back to base 16,
# >>> cvbase.b2b(16,255,a)
# 
10BF31704FAB5B1F03F9B68B1F39A66521B1841F1481697F8E12B7F7DDD6E3D7248D965B1CD45E2114
# length before conversion 82
# J¦+dR + +?+¦ô;ë++vc v@?8ótx+-¦¬5S+z?+-  %ê
# length avter conversion 41
# 
10BF31704FAB5B1F03F9B68B1F39A66521B1841F1481697F8E12B7F7DDD6E3D7248D965B1CD45E2114
# length after converting back 82
# notice the 50% compression of your data from base 16 to base 255?
# notice the 4 times increase of your data from base 16 to base 2?
# Going from base 10 to base 255 gives 41.6% compression.
# Try cvbase.b2b(10,255,((10**1000-1)/9)**2)
# keep in mind, we are compressing numbers, not text. Text is a different 
matter.
# However if you have a billion digits of Pi you want to transmit via modem 
to your
# girl friend, you will cut your phone expense by 58.4% by compressing and 
sending
# algorithm.
#                Have fun in the facinating world of numbers
#

def b2b(r1,r2,n):             #Convert n in base r1 to base r2 and back
    import string
    print n
    print "length before conversion ",len(str(n))
    x = base(r1,r2,n)
    print x
    print "length after conversion ",len(str(x))
    n = base(r2,r1,x)
    print n
    print "length after converting back ",len(str(n))

def base(r1,r2,num):       #This is the crown jewel of the routine where we
    import string          #use base 10 as a pivot to go from base r1 to r2.
    digits=""
    for j in range(0,10):        #We collect the ascii characters we want to 
use.
          digits = digits + chr(j+48)#We can use them all but will not be 
able to print
    for j in range(10,36):
          digits = digits + chr(j+55)
    for j in range(36,52):
          digits = digits + chr(j-4)
    for j in range(52,59):
          digits = digits + chr(j+6)
    for j in range(59,224):
          digits = digits + chr(j+32)
    for j in range(224,256):
          digits = digits + chr(j-224)
    num1 = str(num)               #the first few. The 48-255 as my 
selection.
    ln  = len(num1)
    dec = 0
    for j in range(ln):
          asci = string.index(digits,num1[j]) #Get the ascii code of num
          temp   = r1**(ln-j-1) #Get the decending powers of the decimal 
encoded
          dec += asci*temp      #Convert the num in r1 to decimal
    RDX = ""                    #Init the storage string
    j=-1
    dec2=dec
    while dec2:                 #get number of digits in decimal
         dec2 = dec2 / r2
         j = j+1
    while j >= 0:
          pwr = r2**j           #This is just going from base 10 to another 
base.
          Q   = dec // pwr      #converting the  decimal number to r2 which 
in effect
          dec = dec % pwr       #converting num in base r1 to a number in 
base r2.
          RDX = RDX + digits[Q]
#         RDX = RDX + str(Q)+","  #like Maple. Using this will allow any 
base conv.
          j-=1
    return RDX



------=_NextPart_000_65c4_673f_62d1--




More information about the Tutor mailing list