Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23160

Why is bytearray() making 16 bit numbers

$
0
0

Im writing some code to make .8xp files for a TI-84 and the end of the file is supposed to have a two byte checksum but the first part is being written as a 16 bit number that has no correlation with what it should be

The code isn't the most efficient but at this point im just trying to get something working

Heres the code:

import os

os.system('cls')

inp = input('code file : ')

outp = input('output file : ')

os.system('cls')

file = open(inp,'r')

lines = file.readlines()

j = 0

prgm = " "

start = 0x9d95

tag = 0

strs=[]

mem=['ORG','94 9D']

#c3 unconditional jump

while j < len(lines) :

item = lines[j]item = item.upper()if item[:5] == 'DEC,B' :    l = item[5:]    prgm += '05 '    start += 1if item[:6] == 'CM,A,B' :    l = item[6:]    prgm += 'B8 '    start += 1if item[:5] == 'INC,A' :    l = item[5:]    prgm += '04 '    start += 1if item[:4] == 'LD,B' :    l = item[4:]    prgm += '06 '    prgm += ((2-len(str(hex(int(l)))[2:]))*'0')+str(hex(int(l)))[2:] +''    start += 2if item[:4] == 'LD,A' :    l = item[4:]    prgm += '3E '    prgm += ((2-len(str(hex(int(l)))[2:]))*'0')+str(hex(int(l)))[2:] +''    start += 2if item[:4] == 'JMP,' :    l = item[4:]    prgm += 'C3 '    prgm += mem[mem.index(l)+1] +''    start += 3if item[:4] == 'JNZ,' :    l = item[4:]    prgm += 'C2 '    prgm += mem[mem.index(l)+1] +''    start += 3if item[-3:] == '()\n' :    mem.append(item[:-3])    pl = str(hex(start))[2:]    place = pl[2:] +''+ pl[:-2]    mem.append(place)if item[:4] == 'OUT:' :    prgm += '21 '+'['+str(tag)+'] '    tag += 1    prgm += 'EF 0A 45 '    prgm += 'EF 2E 45 '    strs.append(item[4:])    start += 9if item[:4] == 'OUT;' :    prgm += '21 '+'['+str(tag)+'] '    tag += 1    prgm += 'EF 0A 45 '    strs.append(item[4:])    start += 6j += 1

prgm += 'C9 '

start += 1

j = 0

for st in strs :

pl = str(hex(start))[2:]place = pl[2:] +''+ pl[:-2]prgm =prgm.replace('['+str(j)+']',place.upper())start -= 1j += 1for let in st :    if ord(let) > 32:        prgm += (str(hex(ord(let)))[2:]).upper()+''        start += 1start += 2prgm += '00 '

prgm = prgm.replace('','')

file maker

comment = 'MADE IN PYTHON'

name = 'FILE'

signiture = [42,42,84,73,56,51,70,42,26,10,0]

rnd = 0

header = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

meta_body = [0,0]

rnd2 = 0

bodycs = [0,0]

file_name = [0,0,0,0,0,0,0,0]

rnd3 = 6

arch = 0

body_len = [0,0]

data = [187,108]

bl = len(prgm)

for let in prgm :

data.append(ord(let))

meta_body[0] = (bl+19)%256

meta_body[1] = (bl+19)>>8

bodycs[0] = (bl+2)%256

bodycs[1] = (bl+2)>>8

body_len[0] = bl%256

body_len[1] = bl>>8

j = 0

for i in name :

file_name[j] = ord(i)j += 1

j = 0

for i in comment :

header[j] = ord(i)j += 1

d = []

for i in signiture :

d.append(i)

for i in header :

d.append(i)

d.append(0)

for i in meta_body :

d.append(i)

d.append(13)

d.append(0)

for i in bodycs :

d.append(i)

d.append(5)

for i in file_name :

d.append(i)

d.append(0)

d.append(0)

for i in bodycs :

d.append(i)

for i in body_len :

d.append(i)

for i in data :

d.append(i)

check_sum = 0

for i in range(55,len(d)) :

check_sum += d[i]

os.system('cls')

a = int((('000'+str(hex(check_sum))[2:])[-4:])[2:],16)

print(a)

d.append(0x94)

d.append(check_sum>>8)

data = bytearray(d)

o = open(outp,'wb')

o.write(data)

o.close()

print(('000'+(str(hex(check_sum))[2:]))[-4:])

print('FILE : '+str(outp))

file = open(outp,'r');f = file.read();items = []

for i in f :

l = str(hex(ord(str(i))))[2:]items.append((2-len(l))*'0'+l)

j = 0

end = ''

for i in items :

print(i,end='')if int(i,16) != 10 and i != 0x9d:    end += chr(int(i,16))j += 1if j%8 == 0 :    print('] '+end)    end = ''

while j%8 != 0 :

print('00',end='')j += 1

print('] '+end)


I know its terrible lol

For the input file all i had is

out;@

Which is just a printing function

The checksum is 0x0694 so the last 2 bytes should be 0x94 and 0x6 but its actual output is 0x201d and 0x6And no matter what I do it doesn't change im hoping someone with much more experience with me could explain why bytearray is outputting non byte values


Viewing all articles
Browse latest Browse all 23160

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>