#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    1
    Rep Power
    0

    Encoding chars with Python


    Hi

    I am trying to encode a set of chars for transmission down a wire.

    At the other end I want to unencode so I can see the original chars again. The way I am doing this is by taking character input from the keyboard one by one and then turning these into their ascii values.

    I am then taking the first char left shifting by 8 (multiply by 256 etc) and then adding the second to this then shifting again and adding until I have a long integer which i can then encrypt.

    I have two problems. The first is a warning, when i enter the chars 'a', 'b', 'c', 'd', 'e', 'f': test8.py:10: FutureWarning: x<<y losing bits or changing sign will return a long in Python 2.4 and up
    tot = (((((((((val1 << 8) + val2) << 8) + val3) << 8) + val4) << 8) + val5) << 8) + val6


    My second problem is that when i decode i only receive c d e f. In other words the last two chars to come out dont!

    Here is my code:

    val1 = ord((raw_input("Enter char 1: ")))
    val2 = ord((raw_input("Enter char 2: ")))
    val3 = ord((raw_input("Enter char 3: ")))
    val4 = ord((raw_input("Enter char 4: ")))
    val5 = ord((raw_input("Enter char 5: ")))
    val6 = ord((raw_input("Enter char 6: ")))

    mask = 255

    tot = (((((((((val1 << 8) + val2) << 8) + val3) << 8) + val4) << 8) + val5) << 8) + val6

    char6 = tot & mask

    temp = tot - char6 >> 8

    char5 = temp & mask

    temp = temp - char5 >> 8

    char4 = temp & mask

    temp = temp - char4 >> 8

    char3 = temp & mask

    temp = temp - char3 >> 8

    char2 = temp & mask

    temp = temp - char2 >> 8

    char1 = temp & mask

    print chr(char1)
    print chr(char2)
    print chr(char3)
    print chr(char4)
    print chr(char5)
    print chr(char6)

    Any ideas?

    Thanks

    Eddie
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Regensburg, Germany
    Posts
    147
    Rep Power
    16
    Use long numbers:
    Code:
    val1 = long(ord((raw_input("Enter char 1: "))))
    val2 = long(ord((raw_input("Enter char 2: "))))
    ...
    This will result in long numbers for all subsequent operations. Otherwise you are limited to 32-bit integers.

    You may want to use the struct module instead. With the struct module, you can convert 4-char or 8-char strings easily:
    Code:
    import struct
    
    # 4-char string
    tot = struct.unpack("l", "abcd")
    ...
    char4 = struct.pack("l", tot)
    
    # 8-char string
    long_tot = struct.unpack("q", "abcdefgh")
    ...
    char8 = struct.pack("q", long_tot)
    If a string is shorter than 4 or 8 characters, pad it with space (or another pad char of your choice).

    If it's encryption you need, the Python Cryptography Toolkit at http://www.amk.ca/python/code/crypto.html might be helpful.
  4. #3
  5. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    This task has a more general problem to do with character encoding. Very few computers actually use ASCII for keyboard/display work because it is too limited

    I think I'm right to say Windows uses UTF-16 and Linux uses a whole variety of encodings depending on where the user lives and the version of Linux

    This is one good use for UNICODE. At least there can be a common scheme for exchange then the details of displaying the message are left to conversion routines.

    grim

IMN logo majestic logo threadwatch logo seochat tools logo