1

I want to write to a binary file the content of a vbscript variable that is a number. Is there any way to access the binary representation as a byte array? ( of integers, floats, etc). I have been trying to play with: Adodb.recordset object, append a number field and then read it, but it didn't work.

lc26
  • 23
  • 4

1 Answers1

0

Here you go:

http://www.motobit.com/tips/detpg_binarytostring/

Here is another thread discussing it too:

Read and write binary file in VBscript

intYourVar = 255
hexYourVar = HEX(intYourVar)
binYourVar = MultiByteToBinary(hexYourVar)

Function MultiByteToBinary(MultiByte)
  ' 2000 Antonin Foller, http://www.motobit.com
  ' MultiByteToBinary converts multibyte string To real binary data (VT_UI1 | VT_ARRAY)
  ' Using recordset
  Dim RS, LMultiByte, Binary
  Const adLongVarBinary = 205
  Set RS = CreateObject("ADODB.Recordset")
  LMultiByte = LenB(MultiByte)
  If LMultiByte>0 Then
    RS.Fields.Append "mBinary", adLongVarBinary, LMultiByte
    RS.Open
    RS.AddNew
      RS("mBinary").AppendChunk MultiByte & ChrB(0)
    RS.Update
    Binary = RS("mBinary").GetChunk(LMultiByte)
  End If
  MultiByteToBinary = Binary
End Function
Community
  • 1
  • 1
Nathan Rice
  • 3,091
  • 1
  • 20
  • 30
  • Thanks. That actually works for strings but my number is written in ASCII. I was looking for a way to get, fir the number 255 for example, the 4 byte binary array: 0xFF000000 – lc26 May 08 '13 at 12:24
  • Ahh I see the dilemma now. I updated the code in the example. – Nathan Rice May 08 '13 at 16:24