Semi-colons end lines in Python.
Ohhh, you mean semicolons in general. I was looking for a string of four semicolons. Smile Python doesn't require them, but I'm a C programmer, so I put them by force of habit. Souvik, Python relies on whitespace to mark line endings, so semicolons are not necessary.
I never used them and never saw them being used so I acted strange Razz
ScoutDavid wrote:
I never used them and never saw them being used so I acted strange Razz
Now that we got passed that (protip: try Googling "python semicolons"), did you try using my routine.
KermMartian wrote:
ScoutDavid wrote:
I never used them and never saw them being used so I acted strange Razz
Now that we got passed that (protip: try Googling "python semicolons"), did you try using my routine.



Code:
def mybytearray(thisblist):                           #this stunningly awkward hack is for 2.x/3.x compat
   if sys.version[0] == '2':
      mystr = ""
      for i in range(0,len(thisblist)):
         mystr += chr(int(thisblist[i]));
      return mybytes(mystr,encoding="ascii");
   else:
      return bytearray(thisblist);


I don't need it anymore for creating files, I got over it.

My current problem is converting hex code to strings.

Like 68656c6c6f to hello.
In what format is the hex code? A string like "6865..."? A byte array like b[0x68, 0x65, ...]?
Couple things I see here.

Code:
def mybytearray(thisblist):                           #this stunningly awkward hack is for 2.x/3.x compat
   if sys.version[0] == '2':
      mystr = ""
      for i in range(0,len(thisblist)):
         mystr += chr(int(thisblist[i]));
      return mybytes(mystr,encoding="ascii");
   else:
      return bytearray(thisblist);

If you have Python 2.7, you get bytearrays for free, no hacks required. If you want to support older versions yet, I think it would be cleaner to do something like this in your module's globals:

Code:
try:
    bytearray()
except NameError:
    class bytearray(object):
        """Implements the bytearray features you need"""
        pass

For converting integers to a string (assuming you have a string of numbers maybe), I'd iterate and concatenate using chr. Something like the following:

Code:
(i, s) = (0, '68656c6c6f')
s_out = ''
while i < len(s):
    s_out += chr(s[i:i+2])
    i += 2

(wftspacing? I formatted it fine)
I question "chr(s[i:i+2])". Aren't you trying to chr() a string there?
Er, yeah. Make that chr(int(s[i:i+2],16)).
Tari wrote:
Er, yeah. Make that chr(int(s[i:i+2],16)).
Much better, I agree with that. Scout, does that all make sense to you?
I got it, but I don't need to create bytearrays now, I've done that part in Assemblex. My problem is just counting bytes.

@KermM: It's a string like:


Code:

str1 = "464782"
So is Tari's solution not needed? It looks like you're just asking for len(str1)/2 to me, or perhaps:


Code:
if 2*math.floor(len(str1)/2) != 0:
    print "ERROR: string not multiple of two nibbles!"
    sys.exit(-1)
bytecount = len(str1)/2
Why not:

Code:
if len(str1)%2 == 1:
    print "ERROR: string not multiple of two nibbles!"
    sys.exit(-1)
bytecount = len(str1)/2
Yeah, that's would be the better way to do it, thanks, Shaun. Smile I've gotten too used to languages like TI-BASIC that have no modulus operator.
I have a Python question: how to sum bytes.

For the checksum I need the sum of all the bytes in the data section (which includes the assembly code and the variable header, right?).

This is my old code:


Code:

hexArray = [dataSection[x:x+2] for x in xrange(0,len(dataSection),2)] #Split dataSection in groups of 2 chars (bytes)

sumo=0
for i in range(0,len(hexArray)):                                                              #Loop through hex array
   sumo += int(str(hexArray[i]),16)                                                   #Add the integer value
         
checksum = str(sumo[2]+sumo[3]+sumo[0]+sumo[1])                         #Little Endian


This is not working though, the value returned is not the correct code(I think).

Any idea if this actually is correct or it doesn't work for some reason I can't see?
This should give you an error, I would think, since sumo is an integer and you're indexing into it.

Code:
hexArray = [dataSection[x:x+2] for x in xrange(0,len(dataSection),2)] #Split dataSection in groups of 2 chars (bytes)

sumo=0 
for i in range(0,len(hexArray)):                                                              #Loop through hex array
   sumo += int(str(hexArray[i]),16)                                                   #Add the integer value
         
checksum = str(sumo)[2]+str(sumo)[3]+str(sumo)[0]+str(sumo)[1]                        #Little Endian
And does that work?
merthsoft wrote:
And does that work?
Question seconded. Are you posting the code because it doesn't work and you don't understand why not, or because you want us to check if it will work or not?
If this is the code of a certain program:

2A2A54493833462A
1A0A0046696C6520
67656E6572617465
6420627920576162
6269745369676E00
0000000000000000
000000000022000D
001100064D594649
4C45000000001100
0F00BB6DEF404521
0500EF0745EF7249
C97A08

Is this the data section?:

0D
001100064D594649
4C45000000001100
0F00BB6DEF404521
0500EF0745EF7249
C9

I need to know that so I can know if I'm using the right values in my getCheckSum( function.


Code:
def getCheckSum(dataSection):
   hexArray = [dataSection[x:x+2] for x in xrange(0,len(dataSection),2)]

   sumo=0   
   for i in range(0,len(hexArray)):       
      sumo += int(str(hexArray[i]),16)
           
   checksum = str(sumo)[2]+str(sumo)[3]+str(sumo)[0]+str(sumo)[1]
   return checksum
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
» Goto page Previous  1, 2, 3, 4, 5, 6  Next
» View previous topic :: View next topic  
Page 4 of 6
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement