user4696550
user4696550

Reputation:

python:words in octals and ascii

I was trying to make a header file which is used for converting words into different numeric types(or so) and from such numeric types to words.For example the word 'hello' to the numeric type hexadecimal.

I have completed the work of hex and binary.I also managed to make a string to ascii converter as well.

This is my code:

def string_hex(string):
    return ':'.join(format(ord(c), 'x') for c in string)

def hex_string(hexa):
    hexgen = (hexa[i:i+2] for i in range(0, len(hexa), 2))
    return ''.join(chr(eval('0x'+n)) for n in hexgen)

def string_bin(string):
    return ':'.join(format(ord(c), 'b') for c in string)

def bin_string(binary):
    bingen = (binary[i:i+7] for i in range(0, len(binary), 7))
    return ''.join(chr(eval('0b'+n)) for n in bingen)

def string_ascii(string):
    return ':'.join(str(ord(c)) for c in string)

Well I tried as much as I could to convert the ascii file to string with the help of chr() function. I did it like this:

>>> from my_convert import *
>>> string_ascii('hello')
>>> '104:101:108:108:111'
>>> a = 104101108108111
>>> chr(a)
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    chr(a)
 OverflowError: Python int too large to convert to C long

To use the for loop and that join thingy I didn't knew exactly how much numbers there would be(I could see that there are 2 digit numbers and 3 digit numbers).So I was really confused.

But it isn't working.And also I don't have any idea to convert such strings to octals or from octals to strings.My need is to make a 'ascii to string' converter, a 'string to octal' converter and a 'octal to string' converter.Any help would be appreciated.

Upvotes: 0

Views: 1099

Answers (2)

theunixdisaster
theunixdisaster

Reputation: 45

Guys I think I got an answer for this.This one works even if there is a ':' between numerical values.

# string to hexadecimal converter

def string_hex(string):
    return ':'.join(format(ord(c), 'x') for c in string)

# hexadecimal to string converter.

def hex_string(hexa):
    if ':' in hexa:
        a = hexa.split(':')
        hexa = ''.join(a)
    hexgen = (hexa[i:i+2] for i in range(0, len(hexa), 2))
    return ''.join(chr(eval('0x'+n)) for n in hexgen)

# string to binary converter

def string_bin(string):
    return ':'.join(format(ord(c), 'b') for c in string)

# binary to string converter

def bin_string(binary):
    if ':' in binary:
        a = binary.split(':')
        binary = ''.join(a)
    bingen = (binary[i:i+7] for i in range(0, len(binary), 7))
    return ''.join(chr(eval('0b'+n)) for n in bingen)

# string to ascii converter

def string_ascii(string):
    return ':'.join(str(ord(c)) for c in string)

# ascii to string converter

def ascii_string(asciii):
    if ':' in asciii:
        b = asciii.split(':')
        asciii = ''.join(b)
    b = ''
    c = ''
    i = 0
    while i < len(asciii):
        b += asciii[i]
        if chr(int(b)).isalpha():
            c+=chr(int(b))
            b = ''
            i+=1
        else:
            i+=1
    return c

# string to octal converter

def string_oct(string):
    return ':'.join(format(ord(c), 'o') for c in string)

# octal to string converter

def oct_string(octa):
    if ':' in octa:
        a = octa.split(':')
        octa = ''.join(a)
    octgen = (octa[i:i+3] for i in range(0, len(octa), 3))
    return ''.join(chr(eval('0o'+n)) for n in octgen)

Upvotes: 1

Ethan Furman
Ethan Furman

Reputation: 69258

--> string_hex('Hello')
'48:65:6c:6c:6f'

--> hex_string(string_hex('Hello'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in hex_string
  File "<stdin>", line 3, in <genexpr>
  File "<string>", line 1
    0x:6
     ^
SyntaxError: invalid token

These functions should be mirrors of each other -- you should be able to hex_string a hex_string and get what you started with.

The problem is that your mirror functions (hex_string, bin_string, ascii_string, etc), are not taking the same format that the original functions produce; in other words, string_hex('Hello') produces '48:65:6c:6c:6f', but hex_string requires '48656c6c6f' to work.

Fix your *_string functions to work on "words" seperated by :, and you'll find the difficulty of variable sized words in the ascii_string function disappear.

Upvotes: 1

Related Questions