Antoni Gual Via
2022-10-08 16:45:17 UTC
Hello. I'm experimenting on creating bmp files in VBScript and I have good results writing byte to byte with CHR. The idea is to speed it up writing two bytes at a time using CHRW and an UTF16 Stream. The code below tries to test the concept by writing random long values, then reading them from file and comparing with the original values. It does'nt work, I have probably missed something in my code dealing with signed values. Could you give it a look?
fn=CreateObject("Scripting.FileSystemObject").GetSpecialFolder(2)& "\testwchr.bin"
Function long2wstr( x)
Dim k1,k2,x1
k1= CInt ((x And &h7fff) Or (&H8000 And ((X And &h8000)<>0)))
'k2= ((X and &h7fff0000 ) \&h10000)
k2=((X And &h7fffffff) \ &h10000) Or (&H8000 And ((X And &h80000000) <>0 ))
long2wstr=chrw(k1) & chrw(k2)
End Function
Function wstr2long(s)
x1=AscW(mid(s,1,1))
'xx1=x1-(65536 *(x1<0))
x2=AscW(mid(s,2,1))
wstr2long=CLng(x2*65536+x1)
End Function
Function rndlong() rndlong=CLng(4294967296* rnd()-2147483648) :End Function
Dim a(1000)
With CreateObject("ADODB.Stream")
.Charset = "UTF-16LE" 'o "UTF16-BE"
.Type = 2' adTypeText
.open
Randomize 1
For I=0 To 1000
a(i)=rndlong
.writetext long2wstr(a(i))
Next
.savetofile fn,2
.close
'now read the file to see if ADODB has changed anything
.open
.loadfromfile fn
.position=2 'skip bom
cnt=0
For I=0 To 1000
j= wstr2long(.readtext (2))
If j<>a(i) Then WScript.Echo a(i),j:cnt=cnt+1 ' print unmatching pairs
Next
WScript.Echo cnt 'should print 0 and nothing else
.close
End With
fn=CreateObject("Scripting.FileSystemObject").GetSpecialFolder(2)& "\testwchr.bin"
Function long2wstr( x)
Dim k1,k2,x1
k1= CInt ((x And &h7fff) Or (&H8000 And ((X And &h8000)<>0)))
'k2= ((X and &h7fff0000 ) \&h10000)
k2=((X And &h7fffffff) \ &h10000) Or (&H8000 And ((X And &h80000000) <>0 ))
long2wstr=chrw(k1) & chrw(k2)
End Function
Function wstr2long(s)
x1=AscW(mid(s,1,1))
'xx1=x1-(65536 *(x1<0))
x2=AscW(mid(s,2,1))
wstr2long=CLng(x2*65536+x1)
End Function
Function rndlong() rndlong=CLng(4294967296* rnd()-2147483648) :End Function
Dim a(1000)
With CreateObject("ADODB.Stream")
.Charset = "UTF-16LE" 'o "UTF16-BE"
.Type = 2' adTypeText
.open
Randomize 1
For I=0 To 1000
a(i)=rndlong
.writetext long2wstr(a(i))
Next
.savetofile fn,2
.close
'now read the file to see if ADODB has changed anything
.open
.loadfromfile fn
.position=2 'skip bom
cnt=0
For I=0 To 1000
j= wstr2long(.readtext (2))
If j<>a(i) Then WScript.Echo a(i),j:cnt=cnt+1 ' print unmatching pairs
Next
WScript.Echo cnt 'should print 0 and nothing else
.close
End With