Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


RE: How do I? byte array converted to long, to date...

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,40662
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> RE: How do I? byte array converted to long, to date...
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: <<   < prev  1 [2] 3   next >   >>
Login
Message << Older Topic   Newer Topic >>
 RE: How do I? byte array converted to long, to date... - 12/11/2006 5:44:47 AM   
  ehvbs

 

Posts: 2220
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi ebgreen,

do you start your play with

perl -V | grep int
    hint=recommended, useposix=true, d_sigaction=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='__int64', lseeksize=8

(in reply to ebgreen)
 
 
Post #: 21
 
 RE: How do I? byte array converted to long, to date... - 12/11/2006 5:49:22 AM   
  TNO


Posts: 1397
Score: 16
Joined: 12/18/2004
From: thenewobjective.com
Status: online
yeah, closest I can get to a solution is a .NET one....and a javascript one for a 4bit byte array...I was trying to avoid using COM components as well if they aren't inherent to the OS. hmm....

_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch

(in reply to ebgreen)
 
 
Post #: 22
 
 RE: How do I? byte array converted to long, to date... - 12/11/2006 5:52:44 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: offline
Thanks for the pointer E. No perl at work though. 

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to TNO)
 
 
Post #: 23
 
 RE: How do I? byte array converted to long, to date... - 12/11/2006 6:59:24 AM   
  bofaboy

 

Posts: 16
Score: 0
Joined: 12/9/2006
Status: offline
$foo = pack("c8",68,116,53,22,0,0,17,36); #foo ==  Dt5▬  ◄$
$foo2 = unpack("N",$foo);
$foo2 += 32768; # foo2 = 1148499222
print "foo = ".$foo."\n";
print "foo2 = ".$foo2."\n";
print "foo2: " .scalar(localtime($foo2))."\n"; #prints Wed May 24 12:33:42 2006

output-----
-------------
foo = Dt5▬  ◄$
foo2 = 1148499222
foo2: Wed May 24 12:33:42 2006

I am reading an ADODB Stream as text, therefore all data is returned as text/ascii character codes.

I say ASCII character code values because they are...  You are right that 0 doesn't correspond to any printable number, letter, or special (because it's outside the range of 32-127, but it's still ASCII.)

http://www.asciitable.com/  0 == NULL

P.S. thanks for any help on this.

(in reply to ebgreen)
 
 
Post #: 24
 
 RE: How do I? byte array converted to long, to date... - 12/11/2006 7:23:29 AM   
  ehvbs

 

Posts: 2220
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi bofaboy,

(1) are you using a Perl with 8 bytes long?

(2) if you pack bytes, why do you use "c8" instead of "Cn"?

(3) does your output change, if you do

     $foo = pack("c8",68,116,53,22,  111, 112, 113, 114 );

(in reply to bofaboy)
 
 
Post #: 25
 
 RE: How do I? byte array converted to long, to date... - 12/11/2006 7:45:12 AM   
  bofaboy

 

Posts: 16
Score: 0
Joined: 12/9/2006
Status: offline
The C8 represents 8 Characters..  based on your input that can get changed to C4 and 68,116,53,22

which is what I am passing..

However, you are correct..  I am only using the first 4..

$foo = pack("c8",68,116,53,22,0,0,17,36);
$foo = pack("c8",68,116,53,22,111,112,113,114);

result in the same data.

C:\Perl\Scripts>perl cimptime.pl
foo = Dt5▬  ◄$
foo2 = 1148499222
foo2: Wed May 24 12:33:42 2006

C:\Perl\Scripts>perl cimptime.pl
foo = Dt5▬opqr
foo2 = 1148499222
foo2: Wed May 24 12:33:42 2006

My bad.  I was sure it was a necessary part of the calculations. but it appears not to be after looking it at it again.

Steven 

(in reply to ehvbs)
 
 
Post #: 26
 
 RE: How do I? byte array converted to long, to date... - 12/11/2006 7:47:03 AM   
  bofaboy

 

Posts: 16
Score: 0
Joined: 12/9/2006
Status: offline
The C8 represents 8 Characters..  based on your input that can get changed to C4 and 68,116,53,22

which is what I am passing..

------needs to get changed to---------------

The C8 represents 8 Characters.. 

which is what I am passing..

based on your input that can get changed to C4 and 68,116,53,22

(in reply to bofaboy)
 
 
Post #: 27
 
 RE: How do I? byte array converted to long, to date... - 12/11/2006 7:49:34 AM   
  ehvbs

 

Posts: 2220
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi bofaboy,

I *think*, having 4-bytes longs will cause Perl to ignore any overflow and contuinue
working with the 32 bits.

Are you using Windows/ActiveState Perl? Are you familiar with mixing languages in
.wsf files?

(in reply to bofaboy)
 
 
Post #: 28
 
 RE: How do I? byte array converted to long, to date... - 12/11/2006 7:55:40 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: offline
I can't figure this one out and it is annoying the hell out of me!  

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to bofaboy)
 
 
Post #: 29
 
 RE: How do I? byte array converted to long, to date... - 12/11/2006 8:00:03 AM   
  bofaboy

 

Posts: 16
Score: 0
Joined: 12/9/2006
Status: offline
I'd never thought of that...  but yes, that would work.

Would the syntax be like classic ASP when you try to mix languages?  Yes Windows Activestate perl.

I am familiar with using a "spamsink.vbs" with a "PerlScript" language declaration for some other stuff in Exchange 2000 to load perl's SpamAssassin filter.
<SCRIPT LANGUAGE="PerlScript">
blah....
</SCRIPT>

My only problem in doing multi-language WSF was that at work, my employer doesn't do perl..  or at least at the other sites, they don't do perl..  they only like VBS.  However, they may not have an option.  :)

But it's a simple enough statement that it may never need to be modified, and the only requirement is that it get installed with the PerlScript options.

Thanks,
Steven


(in reply to ehvbs)
 
 
Post #: 30
 
 RE: How do I? byte array converted to long, to date... - 12/11/2006 8:16:45 AM   
  ehvbs

 

Posts: 2220
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Not nice, quick and dirty, and far from a solution for the problem we started with,
but:


      

Output:


      

Good luck! (we need it)

(in reply to bofaboy)
 
 
Post #: 31
 
 RE: How do I? byte array converted to long, to date... - 12/11/2006 9:50:09 AM   
  TNO


Posts: 1397
Score: 16
Joined: 12/18/2004
From: thenewobjective.com
Status: online
lol, dump the perl in a .wsc file and pretend its a .dll   they'll never know the difference 

_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch

(in reply to ehvbs)
 
 
Post #: 32
 
 RE: How do I? byte array converted to long, to date... - 12/11/2006 4:02:06 PM   
  bofaboy

 

Posts: 16
Score: 0
Joined: 12/9/2006
Status: offline
ok..  so, since we can't do it in VBScript, and we were going to shell out to perl to fix the shortcomings..  any chance someone could help me write a .Net app to do it?  I'm thinking that an exe is more palletable than loading perl on 10 servers.

:)


(in reply to ehvbs)
 
 
Post #: 33
 
 RE: How do I? byte array converted to long, to date... - 12/11/2006 5:25:59 PM   
  TNO


Posts: 1397
Score: 16
Joined: 12/18/2004
From: thenewobjective.com
Status: online
A wsf would be smaller and prettier I think without having to register anything. But I'm not up to speed on perl. Could you write this portion as a stand alone function? in goes the array, out goes the result?

_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch

(in reply to bofaboy)
 
 
Post #: 34
 
 RE: How do I? byte array converted to long, to date... - 12/11/2006 5:38:33 PM   
  bofaboy

 

Posts: 16
Score: 0
Joined: 12/9/2006
Status: offline
Perl is a 13MB download that expands into 26MB-50MB.

registering anything in .NET would be much less obtrusive.  I'm thinking a small 200KB executable file.

The exe I was thinking of would take those 4 numbers as arguments, and return the 1148499222 type of value as an errorlevel or as STDOUT.
Something along the lines of this:  (this does not work and I am by no means anywhere close to anything except danger when I try to code VB.NET so far, so laugh freely, heartily, and at my expense ).


Module
dayssinceepoch
Sub Main()
Dim arguments As [String]() = Environment.GetCommandLineArgs()
Dim arg
Dim x
Dim asciicodes
x = 0
asciicodes =
""

For Each arg In arguments
If x > 0 Then

Console.Write(arg &
" ")
If x = 1 Then

asciicodes = Chr(arg)
Else

asciicodes = asciicodes & Chr(arg)
End If

End If

x += 1
Next


Console.WriteLine()
Console.WriteLine(asciicodes)
 

Dim asciibytes As Byte() = Encoding.ASCII.GetBytes(asciicodes)
 
Dim
this = Encoding.Convert(Encoding.ASCII, Encoding.Unicode, asciibytes)
Dim this2 = Encoding.Convert(Encoding.Unicode, Encoding.BigEndianUnicode, this)

Console.WriteLine(this2)
 
End Sub
End
Module

(in reply to ehvbs)
 
 
Post #: 35
 
 RE: How do I? byte array converted to long, to date... - 12/11/2006 9:46:01 PM   
  TNO


Posts: 1397
Score: 16
Joined: 12/18/2004
From: thenewobjective.com
Status: online
Another vbscript attempt:

If in PERL:

$foo = pack("c8",68,116,53,22,0,0,17,36);
$foo = Dt5?  ?$

Then in vbscript:

Dim Foo
Foo = Chr(68)&Chr(116)&Chr(53)&Chr(22)&Chr(0)&Chr(0)&Chr(17)&Chr(36)

so now $foo should = Foo right?

So now to convert back into a number..

If in PERL:

$foo2 = unpack("N",$foo);
$foo2 = 1148466454

Then in vbscrript all we have to figure out is how to convert Foo into a "Long in Network Order"
So what is a "Long in Network Order"? is this a big endian long we are converting to? So we have to convert the binary of Foo into the binary of a bigendian long, then convert that into base 10 for our final value?

Just thinking out loud, maybe someone can take it from here until I come up with a little more...

_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch

(in reply to bofaboy)
 
 
Post #: 36
 
 RE: How do I? byte array converted to long, to date... - 12/12/2006 12:49:14 AM   
  ehvbs

 

Posts: 2220
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi bofaboy e.a.,

Pardon me/Just ignore the following, if you feel I'm pushy. Please give
me a hint if I misunderstood this problem.

But I'm interested in the problem (as I see it) and I think:

   (a) after bofaboy's "my employer doesn't do perl" using Perl for production
       code can be rated as fallback/'if all else fails' solution only

   (b) after bofaboy's "I am only using the first 4" the problem of fighting
       64 bit long integers is out of the way

   (c) bofaboy's "we can't do it in VBScript" can't be true. VBScript may be
       more like an Scooter compared to Porsches with king size trunks like Perl
       or flying trucks like .NET, but multiplying the elements of an array
       with suitable choosen powers of 256 ... Come on, that may need some
       work to get the byte order right and to avoid overflow, but you don't
       need .NET for that.
       
       There may be BAD errors and UGLY inefficiencies in:

            Function convA4Dbl( a4 )
              Dim aBVals  : aBVals  = Array( 256 ^ 3, 256 ^ 2, 256 ^ 1, 256 ^ 0 )
              Dim dblRVal : dblRVal = 0.0
              Dim nIdx
              For nIdx = 0 To UBound( aBVals )
                  dblRVal = dblRVal + (aBVals( nIdx ) * a4( nIdx ) )
              Next
              convA4Dbl = dblRVal
            End Function

       but working together we should be able to improve it to production
       quality in a short time - if you feel like it, of course.
      
   (d) I wrote a small script to test convA4Dbl(); in contrast to my first
       posting, the relation to the original posting is more explicit and
       other as my last one it's pure VBScript:
      

      

       It still lacks explanations, but I believe I'm writing to experts
       here, who can/would ask questions without hesitation.
      
   (d) What really worries me: AFAIK, the VBScript date functionality is
       based on local time. Adding a certain number of seconds to
         dtFrsDate = DateSerial( 1970, 1,  1 ) + TimeSerial(  0,  0,  0 )
       on different computers in different time zones could lead to slightly (?)
       different results. Please, bofaboy, tell me that you/your employers
       don't care. I would hate to take back my "no (big) problem to do it
       in VBScript"!
      
 

(in reply to TNO)
 
 
Post #: 37
 
 RE: How do I? byte array converted to long, to date... - 12/12/2006 1:01:05 AM   
  bofaboy

 

Posts: 16
Score: 0
Joined: 12/9/2006
Status: offline
I would love to make it happen in VBScript..

I will look at the functions you provided just now, and see if I can make it work.

I am only using .NET as a fallback, and don't want to do any part of it in perl as it would require a download on each server that needs this (~10+)..  that isn't going to fly with my boss.

My estimation that VBScript won't do it, is a culmination of feelings when seeing what the group here has been trying to get done, but having a hard time doing..  Please take no offense here, but there are others telling me that it can't be done in VBScript (due to the nature of the pack/unpack missing in VBscript problem).

If there is a solution, I'd love it.  I don't want to have to depend on outside executables to make this happen and only consider it a last resort step, as people are telling me that .NET can totally do it.

I think I have a few hours today to look at this..  (in about 40 minutes when I get to work and get settled).  :)

Thanks for everyone's help on this.
Steven

(in reply to ehvbs)
 
 
Post #: 38
 
 RE: How do I? byte array converted to long, to date... - 12/12/2006 2:57:30 AM   
  bofaboy

 

Posts: 16
Score: 0
Joined: 12/9/2006
Status: offline
First off:

HOLY MACKERAL AND THANK YOU!!
 
Just an FYI...  the problem with the numbers being slightly off is the application trying to be tricky...

I pared down your code and slightly modified it.  The application doesn't return seconds since 1970, but seconds since 1970 and 32768 seconds..  As well, I added the time zone conversions via WMI..  it will need to be slightly modified to get timezone from the server, but, needless to say, that's easy enough to change..

THANK YOU THANK YOU THANK YOU...  when you first posted this solution a few days ago, I didn't understand it..  thanks for sticking with me and clarifying...  I'll test it more throughout the day just to make sure, but with your help, I think we've claimed success.  Again, Thank you!

slightly modified code output:

offset (seconds): -25200
ehvbs and offset: 1148474031
ehvbs: 1148499231
date with offset: 5/24/2006 12:33:51 PM

slightly modified code:

Option Explicit
On Error Resume Next
Dim offset_sec,rtn
rtn = get_time_zone_offset (offset_sec)
WScript.Echo "offset (seconds): " & offset_sec

WScript.Quit doMain01()

Function doMain01()

  Dim dtFrsDate, dtB, dtE,dblVBSNum,dtNewDate
  dblVBSNum = convA4Dbl( Array(68,116,53,31 ))
  dtFrsDate = DateSerial( 1970, 1,  1 ) + TimeSerial(  0,  0,  0 )
  dtNewDate = DateAdd( "s", dblVBSNum + offset_sec, dtFrsDate )
  WScript.Echo "ehvbs and offset: " & dblVBSNum + offset_sec
  WScript.Echo "ehvbs: " & dblVBSNum
  WScript.Echo "date with offset: " & dtNewDate
  doMain01 = 0
End Function

Function get_time_zone_offset(time_zone_minutes)
Dim wmiobj,tz,tzitem
Set wmiobj = GetObject("winmgmts:\\.")
if Err.Number <> 0 Then
   WScript.Echo "error creating timezone wmi object"
   get_time_zone_offset = 0
   Exit Function
End If
Set tz = wmiobj.ExecQuery("select Bias from Win32_TimeZone")
if Err.Number <> 0 Then
   WScript.Echo "error getting currently set Bias from wmi object"
   get_time_zone_offset = 0
   Exit Function
End If
for each tzitem in tz
   time_zone_minutes = tzitem.Bias * 60
   get_time_zone_offset = 1
   Exit Function
Next
get_time_zone_offset = 0
End Function

Function convA4Dbl( a4 )
Dim aBVals  : aBVals  = Array( 256 ^ 3, 256 ^ 2, 256 ^ 1, 256 ^ 0 )
Dim dblRVal : dblRVal = 0.0
Dim nIdx
For nIdx = 0 To UBound( aBVals )
     dblRVal = dblRVal + (aBVals( nIdx ) * a4( nIdx ) )
Next
convA4Dbl = dblRVal + 32768
End Function


(in reply to ehvbs)
 
 
Post #: 39
 
 RE: How do I? byte array converted to long, to date... - 12/12/2006 3:15:05 AM   
  ehvbs

 

Posts: 2220
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Your are very welcome, bofaboy. You made me happy by your kind words and
by extenting my code with the time zone stuff. I'l study it carefully and
will use it to improve some of my scripts. In addition to that: getting
further/independent testing for concepts/code is always a boon.

Thank you for this gratifying piece of cooperative work!

ehvbs

My output:

cscript convba-01.vbs
offset (seconds): 3600    <--- I like that!
ehvbs and offset: 1148502831
ehvbs: 1148499231
date with offset: 24.05.2006 20:33:51

< Message edited by ehvbs -- 12/12/2006 3:21:45 AM >

(in reply to bofaboy)