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.
strReturn = ""
for i in range(len(string1)):
strTemp = hex(ord(string1[i:i + 1]))
if len(strTemp) == 1:
strTemp = "0" + strTemp
strReturn = strReturn +" "+strTemp
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
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.