Module transcoder.src.transcoder

Software and functions to convert from arbitrary strings to various representations of the individual characters

Expand source code
#!python
"""Software and functions to convert from arbitrary strings to
various representations of the individual characters"""
import colored


def asASCII(data):
    """String to printable ASCII code conversion

    Creates and returns a string containing the ASCII code
    representation of the input string.  For example, with the input
    "ABC", the function will return "65 66 67".

    Args:
        data (string): a string to convert into the hex representation

    Returns:
        string: a string containing the ASCII code representation of the input

    """
    output = ""

    for c in data:
        output += ascii(ord(c))+" "

    return output.strip()


def asHex(data):
    """String to printable hex conversion

    Creates and returns a string containing the hexadecimal
    representation of the input string.  For example, with the input
    "ABC", the function will return "0x41 0x42 0x43".

    Args:
        data (string): a string to convert into the hex representation

    Returns:
        string: a string containing the hex representation of the input

    """

    # Start with an empty string
    output = ""

    # Now for each character...
    for c in data:
        # Add the hex representation of the character to the string
        # The ord function returns the ASCII value of a character
        # The hex function returns the hexadecimal representation of a number
        output += hex(ord(c))+" "

    return output.strip()  # Take off any spare spaces at the start/end


def asOctal(data):
    """String to printable octal conversion

    Creates and returns a string containing the octal
    representation of the input string.  For example, with the input
    "ABC", the function will return "0o101 0o102 0o103".

    Args:
        data (string): a string to convert into the octal representation

    Returns:
        string: a string containing the octal representation of the input

    """
    output = ""

    for c in data:
        output += " "+oct(ord(c))

    return output.strip()


def asBinary(data):
    """String to printable binary conversion

    Creates and returns a string containing the binarya representation of the input string.  For example, with the input "ABC", the function will return "0b1000001 0b1000010 0b1000011"

    Args:
        data (string): a string to convert into the binary representation
        test ()

    Returns:
        string: a string containing the binary representation of the input

    """

    output = ""

    for c in data:
        output += " "+bin(ord(c))

    return output.strip()


def colourise(text, colour):
        """    Adds colour to the terminal output

    This adds colour to the terminal output in order to make it look more humanly readable.

    Args:
        text (string): a string to have colour added to
        colour (string): a string that states the HTML colour code to the string
    Returns:
        string: a string containing the coloured string

    """
  
        return colored.fg(colour)+text+colored.attr('reset')


   
   


def main():
    """This function is called when this file is executed as a
program. Usually we don't bother putting a docstring in this function,
but I decided to add this one as a simple example. 

Notice that the String is 'triple quoted'. Usually, a String in Python
is bounded by one single or double quote mark. 'Like this' "or like
this". You can also use triples like this one does. It has a few
benefits that sometimes come in handy. In particular, you can easily
include single and double quote characters within it, because it takes
three in a row to signal the end. You can also go across multiple
lines, like I have here.

    """
    print(colourise("Transcoder V0.2 pre-alpha", "#FF0000\n"))
    inputData = input(f"Input data too transcode: ")
    print(f"\nInput: {inputData}\n")
    print(f"Hex {wrappingPrint(asHex(inputData))}\n\n")
    print(f"Octal {wrappingPrint(asOctal(inputData))}\n\n")
    print(f"Binary {wrappingPrint(asBinary(inputData))}\n\n")
    print(f"ASCII {wrappingPrint(asASCII(inputData))}\n\n")

def wrappingPrint(data):
    """Formats the transcoded data to make it more humanly readabe

    Formats the transcoded data into a format that will make it easier to read large strings.

    Args:
        data (string): a string that has been returned from a prior function to be formated

    Returns:
        string: a string containing the formated data to be printed

    """
    data = data.split()
    n = 10
    final = ""
    temp = [data[i * n:(i + 1) * n] for i in range((len(data) + n - 1) // n )]
    i = 0 
    for val in temp:
        final += "\n"+"    0x"+str(i)+": "
        i= i + 1
        for v in val:
            v=v.strip()
            for x in range(len(v),9):
                v = " " + v
            if len(v) == 2 or len(v) == 8 or len(v) == 4:
                v = " " + v
            final += v + "  "
    return final.rstrip()
if __name__ == "__main__":
    main()


# JS: You can ignore the stuff below. It's just for my spell-checker.
#  LocalWords:  Transcoder pre asHex JS inputData hexData  asOctal
#  LocalWords:  octData binData asBinary oct ord

Functions

def asASCII(data)

String to printable ASCII code conversion

Creates and returns a string containing the ASCII code representation of the input string. For example, with the input "ABC", the function will return "65 66 67".

Args

data : string
a string to convert into the hex representation

Returns

string
a string containing the ASCII code representation of the input
Expand source code
def asASCII(data):
    """String to printable ASCII code conversion

    Creates and returns a string containing the ASCII code
    representation of the input string.  For example, with the input
    "ABC", the function will return "65 66 67".

    Args:
        data (string): a string to convert into the hex representation

    Returns:
        string: a string containing the ASCII code representation of the input

    """
    output = ""

    for c in data:
        output += ascii(ord(c))+" "

    return output.strip()
def asBinary(data)

String to printable binary conversion

Creates and returns a string containing the binarya representation of the input string. For example, with the input "ABC", the function will return "0b1000001 0b1000010 0b1000011"

Args

data : string
a string to convert into the binary representation

test ()

Returns

string
a string containing the binary representation of the input
Expand source code
def asBinary(data):
    """String to printable binary conversion

    Creates and returns a string containing the binarya representation of the input string.  For example, with the input "ABC", the function will return "0b1000001 0b1000010 0b1000011"

    Args:
        data (string): a string to convert into the binary representation
        test ()

    Returns:
        string: a string containing the binary representation of the input

    """

    output = ""

    for c in data:
        output += " "+bin(ord(c))

    return output.strip()
def asHex(data)

String to printable hex conversion

Creates and returns a string containing the hexadecimal representation of the input string. For example, with the input "ABC", the function will return "0x41 0x42 0x43".

Args

data : string
a string to convert into the hex representation

Returns

string
a string containing the hex representation of the input
Expand source code
def asHex(data):
    """String to printable hex conversion

    Creates and returns a string containing the hexadecimal
    representation of the input string.  For example, with the input
    "ABC", the function will return "0x41 0x42 0x43".

    Args:
        data (string): a string to convert into the hex representation

    Returns:
        string: a string containing the hex representation of the input

    """

    # Start with an empty string
    output = ""

    # Now for each character...
    for c in data:
        # Add the hex representation of the character to the string
        # The ord function returns the ASCII value of a character
        # The hex function returns the hexadecimal representation of a number
        output += hex(ord(c))+" "

    return output.strip()  # Take off any spare spaces at the start/end
def asOctal(data)

String to printable octal conversion

Creates and returns a string containing the octal representation of the input string. For example, with the input "ABC", the function will return "0o101 0o102 0o103".

Args

data : string
a string to convert into the octal representation

Returns

string
a string containing the octal representation of the input
Expand source code
def asOctal(data):
    """String to printable octal conversion

    Creates and returns a string containing the octal
    representation of the input string.  For example, with the input
    "ABC", the function will return "0o101 0o102 0o103".

    Args:
        data (string): a string to convert into the octal representation

    Returns:
        string: a string containing the octal representation of the input

    """
    output = ""

    for c in data:
        output += " "+oct(ord(c))

    return output.strip()
def colourise(text, colour)

Adds colour to the terminal output

This adds colour to the terminal output in order to make it look more humanly readable.

Args

text : string
a string to have colour added to
colour : string
a string that states the HTML colour code to the string

Returns

string
a string containing the coloured string
Expand source code
def colourise(text, colour):
        """    Adds colour to the terminal output

    This adds colour to the terminal output in order to make it look more humanly readable.

    Args:
        text (string): a string to have colour added to
        colour (string): a string that states the HTML colour code to the string
    Returns:
        string: a string containing the coloured string

    """
  
        return colored.fg(colour)+text+colored.attr('reset')
def main()

This function is called when this file is executed as a program. Usually we don't bother putting a docstring in this function, but I decided to add this one as a simple example.

Notice that the String is 'triple quoted'. Usually, a String in Python is bounded by one single or double quote mark. 'Like this' "or like this". You can also use triples like this one does. It has a few benefits that sometimes come in handy. In particular, you can easily include single and double quote characters within it, because it takes three in a row to signal the end. You can also go across multiple lines, like I have here.

Expand source code
def main():
    """This function is called when this file is executed as a
program. Usually we don't bother putting a docstring in this function,
but I decided to add this one as a simple example. 

Notice that the String is 'triple quoted'. Usually, a String in Python
is bounded by one single or double quote mark. 'Like this' "or like
this". You can also use triples like this one does. It has a few
benefits that sometimes come in handy. In particular, you can easily
include single and double quote characters within it, because it takes
three in a row to signal the end. You can also go across multiple
lines, like I have here.

    """
    print(colourise("Transcoder V0.2 pre-alpha", "#FF0000\n"))
    inputData = input(f"Input data too transcode: ")
    print(f"\nInput: {inputData}\n")
    print(f"Hex {wrappingPrint(asHex(inputData))}\n\n")
    print(f"Octal {wrappingPrint(asOctal(inputData))}\n\n")
    print(f"Binary {wrappingPrint(asBinary(inputData))}\n\n")
    print(f"ASCII {wrappingPrint(asASCII(inputData))}\n\n")
def wrappingPrint(data)

Formats the transcoded data to make it more humanly readabe

Formats the transcoded data into a format that will make it easier to read large strings.

Args

data : string
a string that has been returned from a prior function to be formated

Returns

string
a string containing the formated data to be printed
Expand source code
def wrappingPrint(data):
    """Formats the transcoded data to make it more humanly readabe

    Formats the transcoded data into a format that will make it easier to read large strings.

    Args:
        data (string): a string that has been returned from a prior function to be formated

    Returns:
        string: a string containing the formated data to be printed

    """
    data = data.split()
    n = 10
    final = ""
    temp = [data[i * n:(i + 1) * n] for i in range((len(data) + n - 1) // n )]
    i = 0 
    for val in temp:
        final += "\n"+"    0x"+str(i)+": "
        i= i + 1
        for v in val:
            v=v.strip()
            for x in range(len(v),9):
                v = " " + v
            if len(v) == 2 or len(v) == 8 or len(v) == 4:
                v = " " + v
            final += v + "  "
    return final.rstrip()