Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > microsoft.public.scripting.vbscript > #12464

Using CHRW to speed up writing binary files

Newsgroups microsoft.public.scripting.vbscript
Date 2022-10-08 09:45 -0700
Message-ID <661f1763-6f55-44ce-8360-ca5f9f99aeb5n@googlegroups.com> (permalink)
Subject Using CHRW to speed up writing binary files
From Antoni Gual Via <antonigualvia@gmail.com>

Show all headers | View raw


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  

Back to microsoft.public.scripting.vbscript | Previous | NextNext in thread | Find similar


Thread

Using CHRW to speed up writing binary files Antoni Gual Via <antonigualvia@gmail.com> - 2022-10-08 09:45 -0700
  Re: Using CHRW to speed up writing binary files Antoni Gual Via <antonigualvia@gmail.com> - 2022-10-08 12:19 -0700
    Re: Using CHRW to speed up writing binary files JJ <jj4public@outlook.com> - 2022-10-09 20:26 +0700
  Re: Using CHRW to speed up writing binary files "Mayayana" <mayayana@invalid.nospam> - 2022-10-10 08:06 -0400
    Re: Using CHRW to speed up writing binary files Antoni Gual Via <antonigualvia@gmail.com> - 2022-10-10 08:23 -0700
      Re: Using CHRW to speed up writing binary files "Mayayana" <mayayana@invalid.nospam> - 2022-10-10 17:40 -0400

csiph-web