Python Forum
Trouble converting numbers to characters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble converting numbers to characters
#1
I'm reading numbers from a file which I want to write to a separate file as the ascii hex of the numbers. So, e.g., if I read in ff, I want to write out '0xff'. My code (below; just printing to console for now) works fine for some values, but not for others. Specifically, it can read/write ff (as above), but when it reads 80, it writes "0x20ac". This seems to be because it interprets 80 as a unicode character whose hex value is 0x20ac (8364 decimal). What am I doing wrong? Misusing hex() and/or ord()? Thanks for any tips.

# Open output file

with open("E:\\Karl\\Documents\\Sculpture\\Prime Time\\Digits\\1d_inv_conv.c", 'w') as g:

# Open input file for reading in text mode

    with open("E:\\Karl\\Documents\\Sculpture\\Prime Time\\Digits\\1d_inv2.pbm", 'rt') as f:
        f.read(10)      # Skip header
        while True:
            x = f.read(1)
            if x == '':     # End of file?
                sys.exit()
            print(hex(ord(x)))
Reply
#2
Probably I don't get the problem, but ord() is for single characters:

>>> help(ord)
Help on built-in function ord in module builtins:

ord(c, /)
    Return the Unicode code point for a one-character string.
(END)
This means that you can't use representation of 80 as string for ord() argument:

>>> ord('80')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ord() expected a character, but string of length 2 found


One can do:

>>> print(*(hex(ord(x)) for x in '80'))
0x38 0x30
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
The contents of the part of the file that are producing the erroneous behavior are
FF FF FF 80 03
FF FF FF 00 03
If I run print (ord(x)) on this section, the output is
255
255
255
8364
3
255
255
255
0
3
I'm trying to figure out why that 80 (0x80) is being read as 8364 (0x20ac which is how it gets written to the file, even though print (hex(ord(8364))) throws an error).
Reply
#4
I don't understand how you run ord() on these without error. What I get:

>>> ord(FF)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'FF' is not defined
>>> ord('FF')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ord() expected a character, but string of length 2 found
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
Well, they worked for me except for that exception. I’ve since reworked the code without the ord() and it’s working fine now.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I check if the first X characters of a string are numbers? FirstBornAlbratross 6 1,521 Apr-12-2023, 10:39 AM
Last Post: jefsummers
  Trouble with converting list , dict to int values! faryad13 7 3,750 Sep-04-2020, 06:25 AM
Last Post: faryad13
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,681 May-15-2020, 01:37 PM
Last Post: snippsat
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,724 May-09-2019, 12:19 PM
Last Post: Pleiades
  Trouble converting JSON String to Dictionary RBeck22 7 5,109 Mar-28-2019, 12:12 PM
Last Post: RBeck22
  converting arguments or input numbers Skaperen 8 4,473 Aug-21-2018, 12:17 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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