Python Forum
looking for code to do ord()
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
looking for code to do ord()
#1
i am looking for code that does what ord() does, given a character, returns the integer value of that character, ...except... the same exact code will do the same in python3 ...and... will also do the same for byte character in python3.

or:

i am looking for code to convert bytes to string as well as other code to convert a string to bytes where the code does the same thing in both python2 and python3 and makes the convert with the same element values.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
bytes(), bytearray() - to bytes
str() - to string

The second ( bytearray() ) creates a mutable object.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
in python3 these require an additional argument specifying the encoding,  i need to do this where all byte and character values are in the unsigned range 0..255 and are unchanged during conversions.

i think changing to hex and then back again in the intended type might be the way to go.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
i came up with the following code that seems to work in both python2 and python3 (using version conditionals and testing in most cases):


from __future__ import print_function
from sys import version_info as ver
def bytes_as_bytearray(b):
    if ver[0] < 3:
        return bytearray(b)
    else:
        return bytearray.fromhex(b.hex())
def bytes_as_str(b):
    if ver[0] < 3:
        return ''.join([chr(ord(c)) for c in b])
    else:
        return ''.join([chr(c) for c in b])
def bytearray_as_bytes(b):
    if ver[0] < 3:
        return ''.join([chr(c) for c in b])
    else:
        return bytes.fromhex(b.hex())
def bytearray_as_str(b):
    return ''.join([chr(c) for c in b])
def str_as_bytes(b):
    if ver[0] < 3:
        return bytes(b)
    else:
        return bytes.fromhex(''.join([hex(ord(c))[2:] for c in b]))
def str_as_bytearray(b):
    if ver[0] < 3:
        return bytearray(b)
    else:
        return bytearray.fromhex(''.join([hex(ord(c))[2:] for c in b]))
bs = b'foobar'
ba = bytearray(bs)
print('python %d.%d.%d'%ver[0:3])
print("str_as_bytes('foobar')",'=',repr(str_as_bytes('foobar')))
print("str_as_bytearray('foobar')",'=',repr(str_as_bytearray('foobar')))
print("bytes_as_str(b'foobar')",'=',repr(bytes_as_str(bs)))
print("bytes_as_bytearray(b'foobar')",'=',repr(bytes_as_bytearray(bs)))
print("bytearray_as_str(bytearray(b'foobar'))",'=',repr(bytearray_as_str(ba)))
print("bytearray_as_bytes(bytearray(b'foobar'))",'=',repr(bytearray_as_bytes(ba)))
please be fully aware that bytes and bytearray are intended for UTF-8 and other encoded octet sequences while str is intended for larger Unicode and other large value sequences that need complex encoding and decoding to do proper type conversion.  this code is intended for special cases where needed values are already in other or wrong types and need to be re-expressed in another or the correct type,  they can be used for cases where literals would be expressed wrong or advanced debugging is needed.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020