VBScript error MID function

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
mightycpa
Automation Wizard
Posts: 343
Joined: Mon Jan 12, 2004 4:07 pm
Location: Vienna, VA

VBScript error MID function

Post by mightycpa » Mon Jul 09, 2018 11:36 pm

Hi,

I copied some vbscript code from here: https://www.mjtnet.com/blog/2015/11/11/ ... e-to-utf8/

Then I modified it a little bit so that I could look at the hex and characters in each line, but I don't know if it works yet, I'm getting stuck on a syntax error in the Mid command.

Microsoft says the syntax is right,

https://msdn.microsoft.com/en-us/library/wffts6k3.aspx

I'm thinking empty string. Also, any help in whatever else might be wrong with this script is much appreciated.

Code: Select all

VBSTART

Sub UTFConvert(filename)
Const adTypeBinary = 1
Dim txt
Dim byteValue
Dim line_ra
Dim line
Dim str_Value
Dim str_length
Dim char_ctr
Dim i, j


  Set fso = CreateObject("Scripting.FileSystemObject")
  txt = fso.OpenTextFile(filename, 1, False, -1).ReadAll
  Set stream = CreateObject("ADODB.Stream")
  stream.Open
  stream.Type     = 2 'text
  stream.Position = 0
  stream.Charset  = "utf-8"
  'split into lines
  line_ra = Split(txt, vbCrLf)
  j = 0
  For each line in line_ra
    i = 0
    j = j + 1
    'read each byte
    str_length = Len(line)
    For char_ctr = 1 to str_length
      str_Value = Mid(line,i,1)
      byteValue = Hex(AscB(str_Value))

      MsgBox Cstr(i) & " S:" & str_Value & " H:" & CStr(byteValue)

      i = i+1
    Next
    if j=10 then Exit For
  Next

  stream.WriteText txt
  stream.SaveToFile filename, 2
  stream.Close
  
'byteValue = Right(00 & Hex(AscB(.Read(1))), 2) ' Returns 0A
'WSCript.echo "Value = " & byteValue
End Sub

VBEND


VBRun>UTFConvert,D:\__IBM\_BPS\FDS\fix.fds


MessageModal>Done
"A facility for quotation covers the absence of original thought." - Lord Peter Wimsey

mightycpa
Automation Wizard
Posts: 343
Joined: Mon Jan 12, 2004 4:07 pm
Location: Vienna, VA

Re: VBScript error MID function

Post by mightycpa » Tue Jul 10, 2018 12:45 am

Also, I'd like to write to a separate file. I could use a little help with that too.

Thanks
"A facility for quotation covers the absence of original thought." - Lord Peter Wimsey

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: VBScript error MID function

Post by Marcus Tettmar » Tue Jul 10, 2018 3:14 pm

I think your line 32 should be:

str_Value = Mid(line,char_ctr,1)

At present you are passing in i as the start character position, but i is the line count in the outer loop.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

mightycpa
Automation Wizard
Posts: 343
Joined: Mon Jan 12, 2004 4:07 pm
Location: Vienna, VA

Re: VBScript error MID function

Post by mightycpa » Tue Jul 10, 2018 5:26 pm

Thanks, that got me moving. My next problem has to do with how to get all the bytes. Here a hex representation of part of my file.

34 37 30 35 37 31 33 37 39 30 20 4D 33 42 20 20
20 30 38 32 30 39 33 30 33 32 37 31 51 55 41 4C
49 54 20 20 20 20 20 20 20 20 20 31 36 20 20 20

this code:

Code: Select all

    For char_ctr = 1 to str_length
      str_Value = Mid(line,char_ctr,1)
      byteValue = Hex(AscB(str_Value))
      MsgBox Cstr(char_ctr) & " S:" & str_Value & " H:" & CStr(byteValue) & " C:" & Chr("&H" & byteValue)
      char_ctr = char_ctr+1
    Next
only gets the hex codes that are red (I didn't mark all of them.) So obviously, strValue is 4 bytes and I'm only grabbing the first one.

If I use AscW, it returns the Hex Codes in green below, and their order is reversed:

34 37 30 35 37 31 33 37 39 30 20 4D 33 42 20 20
20 30 38 32 30 39 33 30 33 32 37 31 51 55 41 4C
49 54 20 20 20 20 20 20 20 20 20 31 36 20 20 20

so I get 3734, 3137,3039,4233, etc.

How can I get each hex code, and in the right order?
"A facility for quotation covers the absence of original thought." - Lord Peter Wimsey

mightycpa
Automation Wizard
Posts: 343
Joined: Mon Jan 12, 2004 4:07 pm
Location: Vienna, VA

Re: VBScript error MID function

Post by mightycpa » Tue Jul 10, 2018 9:48 pm

OK, I found it...this is the line I needed to comment out

Code: Select all

char_ctr = char_ctr+1
DUH!
"A facility for quotation covers the absence of original thought." - Lord Peter Wimsey

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts