1

So I have multiple files that in hex editor looks like below:

      Offset(h) 00 01 02 03 04 05

      1o        10 20 02 00 0A 05 (...)

      2o        10 20 53 00 0A 03 (...)

      3o        10 20 22 00 0A 55 (...)

      4o        10 20 12 00 0A 22 (...)

How do I get the 0A value and store into a variable, knowing that is a fixed position? (always 5th byte)

I need to get the 0A value so I compare to other value and make a decision.

I need to do this in VBscript.

Thanks in advance and appreciate any help

skrenato
  • 87
  • 1
  • 8

3 Answers3

2

Reading from a binary file is easy in vbscript as long as you read sequentially from the beginning to the end.

Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
Dim BinaryFile : Set BinaryFile = fso.OpenTextFile("content.bin")
BinaryFile.Skip(4)
WScript.echo Hex(Asc(BinaryFile.Read(1))) 'Returns "A"
BinaryFile.Close
Regis Desrosiers
  • 537
  • 3
  • 13
  • You have to be careful when it comes to encoding which is why `ADODB.Stream` is a better option. – user692942 Mar 30 '17 at 09:34
  • 1
    It worked! Thank you very much. But I got curious how it would be with ADODB.Stream... I tried to do, seach a lot, but I have failed. – skrenato Mar 30 '17 at 13:38
  • finally I found out how to use `ADODB.Stream`. To do the same. Plus I needed the hex value to be output in two char – skrenato Apr 05 '17 at 18:24
1

I found out how to use ADODB.Stream to solve this problem:

Const adTypeBinary = 1

Dim byteValue

With CreateObject("ADODB.Stream")
    .Type = adTypeBinary
    .Open
    .LoadFromFile fileName
    .Position = 4 ' could be any byte position
    byteValue = Right(00 & Hex(AscB(.Read(1))), 2) ' Returns 0A
End With

' Print byteValue
WSCript.echo "Value = " & byteValue
skrenato
  • 87
  • 1
  • 8
0

There are other questions/answers on SO regarding reading binary files in VBSCript, perhaps they will help you.

What's important to remember is that a binary file (as is any file) is a continuous stream of bytes. So rather than thinking about the "5th byte" of each line remember it will be the 5th, 10th, 15th etc. byte you are interested in.

This is why your hex viewer has an "Offset" column to show how far through the stream of bytes you are.

Community
  • 1
  • 1
Tony
  • 9,672
  • 3
  • 47
  • 75
  • Not the best example to link, they should be using `ADODB.Stream` which that does not. – user692942 Mar 29 '17 at 22:54
  • @Lankymart - please add additional links if you know of better examples. I must admit my VBScript is rather rusty :) Interestingly the [example in MSDN](https://msdn.microsoft.com/en-us/library/9tk3bdxw.aspx) does not use ADODB. – Tony Mar 29 '17 at 23:42
  • 1
    The example you refer to is in VB.Net, not VBScript. – Regis Desrosiers Mar 29 '17 at 23:52