• PUTing an Integer into a Binary File (VBA)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » PUTing an Integer into a Binary File (VBA)

    Author
    Topic
    #364908

    I need to put a 2 byte integer into a binary file. For the sake of discussion, let’s say the integer is 27. When I use the PUT statement to write it to the binary file, it goes in as 1B 00 hex. I need it to go in as 00 1B. Is there an easy way in VB(A) to invert the MSB and the LSB without additional DLLs? Do I need to get each byte by itself and write it bytewise into the file (seems like the hard way)?

    Can anyone recommend a web site that talks about bit-fiddling in VB?

    Thanks for the help.

    Viewing 1 reply thread
    Author
    Replies
    • #561759

      This seems to work:

      Dim strResult As String
      'convert integer to hex and pad leading spaces with zeros
      strResult = Replace(Format(Hex(yourIntegerVar), "@@@@"), " ", "0")
      ...
      Put #1, , CByte("&H" & Mid(strResult, 1, 2))
      Put #1, , CByte("&H" & Mid(strResult, 3, 2))

      But maybe there is another way that isn’t so manual.

    • #562059

      Integers are actually stored that way on PCs. They will retrieve correctly using a GET.

      I assume you are transferring the file to a non-PC computer and that’s why you want them to be changed.

      This will do it.

      Dim myInteger as Integer
      Dim myByte as Byte

      myByte=myInteger / 256
      Put #?, , myByte
      myByte = myInteger And 255
      Put #?, , myByte

      Hope this helps.

      Kevin Bell

    Viewing 1 reply thread
    Reply To: PUTing an Integer into a Binary File (VBA)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: