Python Forum
convert a character to numeric and back
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
convert a character to numeric and back
#1
if my function gets a single character that is str or bytes or bytearray, there is a simple way to convert its character code. ord() works for all 5 types, so thats easy. going the other way has to be harder because of which type you want to go to. chr() get str.

i want the same type as i originally had because this conversion is because i need to do changes to the character that are done as a numeric code. Unicode is not involved. all characters are ASCII only even for type str.

what's a good way to convert back to the original character type?
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
The type info is lost after ord(). Can't get it back unless store it first.

t = type(c)
n = ord(c)
...later
try:
    t(chr(n), encoding='utf8')
except TypeError:
    chr(n)
Reply
#3
i still have the original value that was passed to ord(). or i could to t=type() as your code starts with.

unfortunately, the bytes() and bytearray() types as functions don't convert number the the ASCII character with that code. and if you use encoding= it expects a string argument (try it).

it looks like i'll need to do:
n=ord(c)
n=...n...
if t is str:
    return chr(n)
if t in (bytes,bytearray):
    return t([n])
that's not bad.
Tradition is peer pressure from dead people

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  SyntaxError: unexpected character after line continuation character siteshkumar 2 350 Jul-13-2020, 07:05 PM
Last Post: snippsat
  How to calculate column mean and row skip non numeric and na Mekala 5 588 May-06-2020, 10:52 AM
Last Post: anbu23
  How do I convert this string back to a list of integers? donmerch 6 821 Apr-05-2020, 06:43 PM
Last Post: donmerch
  Alpha numeric element list search rhubarbpieguy 1 363 Apr-01-2020, 12:41 PM
Last Post: pyzyx3qwerty
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 588 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  are numeric types passed by value or reference? rudihammad 4 571 Nov-19-2019, 06:25 AM
Last Post: rudihammad
  'Age' categorical (years -months -days ) to numeric Smiling29 4 682 Oct-17-2019, 05:26 PM
Last Post: Smiling29
  how to do a numeric sort Skaperen 11 1,281 Jul-12-2019, 09:50 AM
Last Post: Skaperen
  splitting numeric list based on condition python_newbie09 7 2,538 May-27-2019, 03:58 PM
Last Post: python_newbie09
  Replace changing string including uppercase character with lowercase character silfer 11 1,756 Mar-25-2019, 12:54 PM
Last Post: silfer

Forum Jump:

User Panel Messages

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