Author Topic: I give up, I need help with Python  (Read 5582 times)

0 Members and 1 Guest are viewing this topic.

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: I give up, I need help with Python
« Reply #15 on: February 13, 2011, 12:58:15 pm »
1. Yeah, eight nibbles, 4 bytes, bad calculation XD
2. I know 1024 is the size, because:
...0010240D00180...

The 1024 is at the location where the size should be and I also added a print line to print the little endian value of the data section and it returned '1024'.

See my edit above. Use hex(littleEndian)[2:] instead :)

EDIT: Dammit, new page >:(

So you don't have to click back, here's what I said:

And replace str(littleEndian) with hex(littleEndian)[2:]. Since you're dealing with a hex string, you need that to be a hex string too. (The [2:] is because hex( converts it into '0xHH' form.)
« Last Edit: February 13, 2011, 01:02:44 pm by Deep Thought »




Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: I give up, I need help with Python
« Reply #16 on: February 13, 2011, 01:16:14 pm »
210100C9
It returned this:
Code: [Select]
2A2A54493833462A
1A0A0046696C6520
67656E6572617465
6420627920576162
6269745369676E00
0000000000000000
00000000004000D0
018000650524F475
2414D00000018001
600BB6D210100C9C
E00B

4000 is the size, but it was supposed to be 0400. So, am I missing a switch in the code?

Code: [Select]
def to_binary(hex_string):
ints = [int(hex_string[i:i+2], 16) for i in range(0,len(hex_string),2)]
return struct.pack('B' * len(ints), *ints)
   
dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "8XP Files (*.8xp)|*.8xp", \
wx.SAVE | wx.OVERWRITE_PROMPT)

if dlg.ShowModal() == wx.ID_OK:

programData = self.inputText.GetValue()

dataLengthBigEndian = len(programData)/2

littleEndian = dataLengthBigEndian %  256 * 256 + (dataLengthBigEndian / 256)


sample8xp = "2A2A54493833462A1A0A0046696C652067656E657261746564206279205761626269745369676E0000000000000000000000000000"""+str(hex(littleEndian)[2:])+"0D0018000650524F4752414D00000018001600BB6D"+programData+"CE0B"

print littleEndian
print dataLengthBigEndian

self.filename=dlg.GetFilename()
self.dirname=dlg.GetDirectory()
filehandle=open(os.path.join(self.dirname, self.filename),'wb')


filehandle.write(to_binary(sample8xp))
filehandle.close()

self.SetTitle('Assemblex - '+self.filename)

dlg.Destroy()

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: I give up, I need help with Python
« Reply #17 on: February 13, 2011, 01:18:07 pm »
Don't bother doing str(hex(. hex( returns a string variable already. Try that, but I'm not sure if that's it...

EDIT: Hey, I figured it out!

Hold on...

EDIT2: Basically, it's this: When you do hex( when the length of data is 4, it returns '0x400'. See the problem? You slice it with [2:], and it becomes '400'. That's still not the value you want. You need to pad it so it's exactly 4 chars long.

A really crude (but simple) way to do it is to do something like

Code: (Python) [Select]
if len(le) == 3:
    le = '0' + le

Then put it in the data.

EDIT3: How about this:

(le if len(le) == 4 else ('0' + le))

where le is the little-endian hex string as found by hex(littleEndian)[2:].

Nice and concise? ;D
« Last Edit: February 13, 2011, 01:28:13 pm by Deep Thought »




Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: I give up, I need help with Python
« Reply #18 on: February 13, 2011, 01:44:48 pm »
Code: [Select]
2A2A54493833462A
1A0A0046696C6520
67656E6572617465
6420627920576162
6269745369676E00
0000000000000000
000000000004000D
0018000650524F47
52414D0000001800
1600BB6D210100C9
CE0B

The size created seems perfect ;D