python ord not working as expected -
so working on move front encoding/decoding assignment software engineering course , when using built in ord() function python 3.3 seems returning wrong value @ point in code.
when want encode coding number 1-120 add code number 128. numbers between 121 , 375 use 2 bytes, first being f9 signify following 1 byte part of code number, , second being actual code number (encoded code # - 128). example 121 f9 00.
when decoding running issue after reading in f9 , moving code decoding second byte, run issue ord function.
my code is:
def decode_num(base_num, input_file): if base_num <=248: #coding if code 1 byte code 1-120(will have been coded 248) return base_num-128 elif base_num == 249: #coding if code 2 byte code, first byte of code 121 second_byte=ord(input_file.read(1)) return second_byte+121
it seems work fine until hits coding 134, should f9 0d. ord(input_file.read(1)) call returns 10 instead of 13 should. have confirmed in mtf file trying decode hexdump show f9 0d running issue. current test case working through occurs 0d second byte of 2 byte code. 0c , work fine, , 0e , ahead work fine.
any ideas @ potentially causing this? or alternative ideas decoding 2 byte code number?
edit: forgot mention mtf files have been encoded in latin-1. if makes difference.
i found out cause of issue. due python , how dealing different styles of encoding. seeing '\r' new line, treating '\r' , '\n' same way. when trying decode 0x0d gives me same result 0x0a.
i able resolve issue specifying newline "" in opening of input file.
input_file = open(input_name, encoding="latin-1", mode="r", newline="")
thanks issue. issue, code acting expected now.
Comments
Post a Comment