OT - convert VB code to Python

Sorry for this OT post.

The following VB code is used to generated a unique ID for an item to be able to exchange between different systems. I think one could do this much simpler, but this is in use so it has to be done this way.

I don't know anything about VB, so I hope there is a kind soul here who knows VB and could help me with converting this code to Python.

Sub Convert2Bin()

  Dim Y As Long

  With fp
    Y = Year(.Datum) - 1980
    fpbin.FP1 = (Y * (2& ^ 20)) + _
                (Month(.Datum) * (2 ^ 16)) + _
                (Day(.Datum) * (2 ^ 11)) + _
                (Hour(.Datum) * (2 ^ 6)) + _
                Minute(.Datum)
    fpbin.FP2 = ((.CreatorID And &H7FF) * (2& ^ 20)) + _
                (.Counter And &HFFFFF)
    If .CreatorID > 2047 Then fpbin.FP2 = -fpbin.FP2
  End With

End Sub

Thanks in advance
Werner

Hello Werner:

I could probably help you with this, but the code is not self-contained.
fp is a reference to some global or module level variable. It appears that
fp.Datum is a VB date object, but I don't know what the other member
variables are. A definition of the fp object would help. Also, there is no
explicit return value, so I assume that the data stored in that other
undefined object, fpbin, is the answer that is being produced.

···

On 9/15/06, Werner F. Bruhin <werner.bruhin@free.fr> wrote:

Sub Convert2Bin()

  Dim Y As Long

  With fp
    Y = Year(.Datum) - 1980
    fpbin.FP1 = (Y * (2& ^ 20)) + _
                (Month(.Datum) * (2 ^ 16)) + _
                (Day(.Datum) * (2 ^ 11)) + _
                (Hour(.Datum) * (2 ^ 6)) + _
                Minute(.Datum)
    fpbin.FP2 = ((.CreatorID And &H7FF) * (2& ^ 20)) + _
                (.Counter And &HFFFFF)
    If .CreatorID > 2047 Then fpbin.FP2 = -fpbin.FP2
  End With

End Sub

http://www.phys.ufl.edu/cms/tfcvs/cgi-bin/cgi/viewcvs.cgi/EmuDAQ/DataFormat/test/asciitobinary.c

finally

http://docs.python.org/lib/module-binascii.html

Werner F. Bruhin wrote:

Sorry for this OT post.

The following VB code is used to generated a unique ID for an item to be able to exchange between different systems. I think one could do this much simpler, but this is in use so it has to be done this way.

I don't know anything about VB, so I hope there is a kind soul here who knows VB and could help me with converting this code to Python.

Sub Convert2Bin()

Dim Y As Long

With fp
   Y = Year(.Datum) - 1980
   fpbin.FP1 = (Y * (2& ^ 20)) + _
               (Month(.Datum) * (2 ^ 16)) + _
               (Day(.Datum) * (2 ^ 11)) + _
               (Hour(.Datum) * (2 ^ 6)) + _
               Minute(.Datum)
   fpbin.FP2 = ((.CreatorID And &H7FF) * (2& ^ 20)) + _
               (.Counter And &HFFFFF)
   If .CreatorID > 2047 Then fpbin.FP2 = -fpbin.FP2
End With

End Sub

Well, the general principle is that you're converting a set of fields to a compressed binary result.

As Michael said, we don't have all the data here. But let's assume you have the following values from your "fp" datatype.
year
month
day
hour
minute
creatorID
counter

Then you can at least say this:
fp1 = ((year-1980) << 20) +
   ((month & 0xF) << 16) +
   ((day & 0x1F) << 11) +
   ((hour & 0x1F) << 6) +
   (minute & 0x3F)
fp2 = ((creatorID & 0x7FF) << 20) + (counter & 0xFFFFF)
if creatorID > 2047:
   fp2 = -fp2

But there's no evidence on how fp1 and fp2 end up in the fpbin object.

       Kent

Micheal,

Thanks for the code you sent me off-line, I think this is getting me going.

Werner

Thanks for the references. I had found some of those but had problem translating the VB stuff as I did not understand it.

Michael's code sample helps and I think I can figure it out from here.

Werner

plumber Idraulico wrote:

···

finally

binascii — Convert between binary and ASCII — Python 3.13.0 documentation

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

Hi Kent,

Thanks.

I think I am about 90% there with the Python code, some problem in converting these values back to the original data, but hopefully I will find my error.

Werner

Kent Quirk wrote:

···

Werner F. Bruhin wrote:

Sorry for this OT post.

The following VB code is used to generated a unique ID for an item to be able to exchange between different systems. I think one could do this much simpler, but this is in use so it has to be done this way.

I don't know anything about VB, so I hope there is a kind soul here who knows VB and could help me with converting this code to Python.

Sub Convert2Bin()

Dim Y As Long

With fp
   Y = Year(.Datum) - 1980
   fpbin.FP1 = (Y * (2& ^ 20)) + _
               (Month(.Datum) * (2 ^ 16)) + _
               (Day(.Datum) * (2 ^ 11)) + _
               (Hour(.Datum) * (2 ^ 6)) + _
               Minute(.Datum)
   fpbin.FP2 = ((.CreatorID And &H7FF) * (2& ^ 20)) + _
               (.Counter And &HFFFFF)
   If .CreatorID > 2047 Then fpbin.FP2 = -fpbin.FP2
End With

End Sub

Well, the general principle is that you're converting a set of fields to a compressed binary result.

As Michael said, we don't have all the data here. But let's assume you have the following values from your "fp" datatype.
year
month
day
hour
minute
creatorID
counter

Then you can at least say this:
fp1 = ((year-1980) << 20) +
  ((month & 0xF) << 16) +
  ((day & 0x1F) << 11) +
  ((hour & 0x1F) << 6) +
  (minute & 0x3F)
fp2 = ((creatorID & 0x7FF) << 20) + (counter & 0xFFFFF)
if creatorID > 2047:
  fp2 = -fp2

But there's no evidence on how fp1 and fp2 end up in the fpbin object.

      Kent

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org