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

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

    A Few Visual Basic to Python Questions


    I just had a few Visual Basic to Python questions that I really need help with. Heres a small list:
    LCase()
    Mid()
    Left() and Right()
    Chr()

    The last thing I need help with is converting the following line of code to Python from VIsual Basic.
    CopyMemory ByVal Result, Value, 2

    I have no clue whatsoever how to get Python to do something like that line. Whatever you can help me with, just show me what to do. Thanks.
  2. #2
  3. A wanna-be guru of some sort
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Either online or offline
    Posts
    625
    Rep Power
    20
    Dude, I dunno VB, but I see those are character and string methods, so browse online docs for those modules...
    Am I supposed to sign here?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    Lawrence, Kansas [KU]
    Posts
    1,559
    Rep Power
    15
    LCase()
    Code:
    >>> g="ABC"
    >>> b = g.lower()
    >>> print b
    abc
    http://www.python.org/doc/current/li...g-methods.html

    Mid(),Left(),Right()
    this is built-in in python
    Code:
    >>> k="ABCDEFG"
    >>> k[0:3]
    'ABC'
    >>> k[2:5]
    'CDE'
    >>> k[-4:-1]
    'DEF'
    chr()
    i am pretty sure it's exactly the same in python as vb.

    Code:
    >>> chr(65)
    'A'
    as for your CopyMemory question...i don't remember ever using that function in vb, but if i find anything on it ill post back.

    <edit>
    if you haven't already been here, it's a nice source of python info.: http://diveintopython.org/toc/index.html
    </edit>
    Last edited by jacktasia; December 4th, 2004 at 07:49 PM.
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    Ok, thanks so much for the help jacktasia, I'll try them out in a minute. As for CopyMemory, I myself don't know how often it gets used, but I do know that all Visual Basic and C++ programemrs use it in their PacketBuffers for sending Packets to Battle.net (I'm only assuming you know what battle.net is).
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    CopyMemory in VB is used to move blocks of data around, similar to C's memcpy function.

    There is no equivalent in Python, since it is not normally used for manipulating memory directly.

    There are other ways of achieving the same effect, for example using the array module to create your memory buffers and using indexing and slicing to copy bytes around.
    e.g.
    Code:
    >>> import array
    >>> a = array.array('c', 'hello world')
    >>> b = array.array('c', 'goodbye cruel')
    >>> a[0:5] = b
    >>> a
    array('c', 'goodbye cruel world')
    Notice that this is slightly different from the CopyMemory function - the characters on the end of 'a' are moved up by the operation instead of overwritten.

    There are other ways of achieving the same thing - you could create your data in strings and use the struct module to convert it to something that could be passed to a C function. There is also the xdrlib module, but I have never used that.

    Dave - The Developers' Coach

IMN logo majestic logo threadwatch logo seochat tools logo