#1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154

    Look Over my Code


    Here are a few things that I need to have looked over. I'm pretty sure that they're close to working only they may have 1 or 2 things wrong with them.
    Original Code:
    Code:
    Public Function StrToHex(ByVal String1 As String) As String
    On Error GoTo EndStr
      Dim strTemp As String, strReturn As String, i As Long
        For i = 1 To Len(String1)
          strTemp = Hex(Asc(Mid(String1, i, 1)))
          If Len(strTemp) = 1 Then strTemp = "0" & strTemp
          strReturn = strReturn & " " & strTemp
        Next i
          StrToHex = strReturn
    EndStr:
     Exit Function
    End Function
    My code:
    Code:
        def StrToHex():
            while i == 1:len(string1)
            strTemp = hex(ord(string1[i:1]))
            if len(strTemp) == 1:
                strTemp = "0"+strTemp
            strReturn = strReturn+" "+strTemp
    Original Code:
    Code:
    Public Function KillNull(ByVal Text As String) As String
        Dim i As Integer
        i = InStr(1, Text, Chr(0))
        If i = 0 Then
            KillNull = Text
            Exit Function
        End If
        KillNull = Left(Text, i - 1)
    End Function
    My code:
    Code:
        def KillNull(): # Pending approval 
            KillNull = KillNull()
            i = chr(0) in text
            if i == 0:
                killnull = text
                pass
            killnull = text[i:1]
    Original code:
    Code:
    Public Sub NullTruncString(ByRef Text As String)
        Dim i As Integer
        
        i = InStr(Text, Chr(0))
        If i = 0 Then Exit Sub
        
        Text = Left(Text, i - 1)
    End Sub
    My code:
    Code:
        def NullTruncString(): # Pending approval 
            i = chr(0) in text
            if i == 0:
                pass
            text = text[i:1]
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    One of the advantages of an interpreted language, especially one which has an interactive listener prompt, is that it is much easier to test an individual function or group of functions in isolation; you don't need to set up a whole test harness for it, you just load a copy of the function into the workspace and then write a few tests cases to see if it behave the way you expect it to.

    That is to say, you'd have had faster results, with less sarcasm, if you'd just run the damn functions and saw that they do not, in fact, work.

    just to take the first one, well, right of the bat I could see that the were some syntax errors, so it was clear you hadn't try testing them yourself. Also, the translation of the VB code was incorrect; you dropped the function argument and the return value, you wrote the while loop incorrectly (BTW, it would be easier to use for i in range(len(string1)):, it think) and made a hash of the indentation.

    Anyway, I fired up DrPython (which I definitely recommend, though I hear Boa Constructor is even better), pasted your code into the editor window, fixed errors I saw, and worked up a simple test. Fixing the obvious problems sbrought out a less obvious one, that the range string1[i:1] should be string1[i:i + 1]; otherwise, it becomes an invalid range once i is greater than zero.
    Code:
    def StrToHex(string1):
    	strReturn = ""
    	for i in range(len(string1)):
    		strTemp = hex(ord(string1[i:i + 1]))
    		if len(strTemp) == 1:
    			strTemp = "0" + strTemp
    			strReturn = strReturn +" "+strTemp
    	return strReturn
    			
    print "'Hello' becomes " + StrToHex("Hello")
    (The last line is the test case, which will run automatically when the script is run in the interpreter).

    Fixing that got the function to the point where the interpreter wouldn't spit it out; however, the test showed that the function was returning a blank string. As a quick sanity check, I dropped into the interactive window, and ran
    Code:
    print(hex(ord('h')))
    This returned the string '0x68', which is correct, but clearly also not what the function in question was geared towards.

    At this point I decided to leave off, as it was clear - as I had said to you already - that Python's idioms, conventions and libraries were too different from VB's to make a direct translation practical. The two hex() functions have the same purpose, but they are not interchangible. If you want to create a function closer to the one in VB, you might want to see how the Python entries for this 'hex dump chrestomathy' did it. HTH.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    Looks like I didn't get round to posting my reply before Firefox crashed..

    *sigh*

    Let me try again quickly...

    Code:
    1 def KillNull(): # Pending approval 
    2         KillNull = KillNull()
    3         i = chr(0) in text
    4         if i == 0:
    5             killnull = text
    6             pass
    7         killnull = text[i:1]
    Line 1: no parameters, so where is the "text" coming from?
    Line 2: Calling itself? That will just loop then crash!
    Line 3: i is commonly a counter variable, so that's a bit confusing, but it will work
    Line 4: The result of "x in y" is True or False, so test i == False isntead for clarity.
    Line 5: right
    Line 6: This does nothing. Pass is just a placeholder
    Line 7: This ignores all text before the null, is that right?
    Line 8: No return command. Now data can't get into the function, or out, so it's a touch pointless.

    -----

    Same points here for the last bit...

    Code:
    1 def NullTruncString(): # Pending approval 
    2         i = chr(0) in text
    3         if i == 0:
    4             pass
    5         text = text[i:1]
    Line 1: Also no parameters
    Line 2: Same about the True False thing
    Line 3: This test does nothing. Pass doesn't exit the function, it just does ... nothing.
    Line 5: Doesn't this do the same as KillNull?
    Line 6: Also no return command...

    Depending on what the code actually needs to do, you could probably replace it all with one of the following:

    Either...

    Remove a null at the start of a string:

    Code:
    def killNullAtOneEnd(text):
        if text.startswith(chr(0)):
            return text[1:]
        else:
            return text
    (or use .endswith() and text[:-1] for the end of the string)

    Or...

    Dump both the functions completely and remove any null in the string:

    Code:
    text = text.replace(chr(0), "")
    Or ...

    Use string.split to get the text either side of the null:

    Code:
    beforeNull, afterNull = text.split(chr(0))
    Last edited by sfb; December 12th, 2004 at 01:54 PM.
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Ok, thanks for the help the both of you, I understand this much better now.

IMN logo majestic logo threadwatch logo seochat tools logo