Byte-operations.

Dan Bishop danb_83 at yahoo.com
Thu May 19 17:14:38 EDT 2005


Dave Rose wrote:
> I hope someone can please help me.  A few months ago, I found a VBS
file,
> MonitorEDID.vbs on the internet.
...[snip]...
> Anyway, the functions from VBS I don't know how to translate to
Python are:
>
> #        location(0)=mid(oRawEDID(i),0x36+1,18)
> #        location(1)=mid(oRawEDID(i),0x48+1,18)

IIRC, the BASIC mid$ function is mid(string, start, length), where the
starting index is 1-based.  The Python equivalent is
string[start-1:start-1+length], so these two lines would convert to

   location = [oRawEDID(i)[54:72], oRawEDID(i)[72:90]]

> #        #you can tell if the location contains a serial number if it
starts
> with 0x00 00 00 ff
> #        strSerFind=chr(0x00) & chr(0x00) & chr(0x00) & chr(0xff)

The "chr" function is the same in Python as in VB.  But unlike VB, you
can use literals for special characters.

   strSerFind = '\x00\x00\x00\xFF'

...[snip]...
> #                tmpver=chr(48+tmpEDIDMajorVer) & "." &
chr(48+tmpEDIDRev)

In python, the string concatenation operator is "+" instead of "&".
However, this line can be written more concisely as:

   tmpver = "%c.%c" % (48 + tmpEDIDMajorVer, 48 + tmpEDIDRev)

But for single-digit integers, chr(48+n) is just an obscure way of
writing str(n), so what it really means is:

   tmpver = "%s.%s" % (tmpEDIDMajorVer, tmpEDIDRev)

> #            if (Byte1 and 16) > 0:
> #                Char1=Char1+4

VB's "and" operator is a bitwise operator; the "if" statement is
testing whether bit 4 is set in Byte1.  The Python equivalent is

   if Byte1 & 0x10:
      Char1 += 4




More information about the Python-list mailing list