Results 1 to 3 of 3
  1. #1
    2 Star Lounger
    Join Date
    Mar 2001
    Colorado, USA
    Thanked 0 Times in 0 Posts

    PUTing an Integer into a Binary File (VBA)

    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.

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Silicon Valley, USA
    Thanked 94 Times in 90 Posts

    Re: PUTing an Integer into a Binary File (VBA)

    This seems to work:
    <pre>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))</pre>

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

  3. #3
    3 Star Lounger
    Join Date
    Aug 2001
    Jeddah, Saudi Arabia
    Thanked 1 Time in 1 Post

    Re: PUTing an Integer into a Binary File (VBA)

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts