Results 1 to 4 of 4

Thread: [RESOLVED] How does VB.Net store variables in a binary file?

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    51

    Resolved [RESOLVED] How does VB.Net store variables in a binary file?

    I have an object variable that I want to store in a binary file. If I give the object variable a number, it will store it without a header. If it is a short string it will store a one-byte header with the length of the string. However, I am not sure what it does with long strings.

    I gave it a 868 byte string (as an example) which is equivalent to
    64 03 in hexadecimal (in my hex editor)

    But the header it gave was:
    E4 06

    Three bits are different. I can understand the 6 turning into an E, but not the rest.

    Also, if you were to store binary data, like say a picture in an object variable would it have a header of some kind?

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: How does VB.Net store variables in a binary file?

    Binary data is just that, ones and zeros. How the ones and zeros are organized and grouped determine the file type or file format; jpg, xml, mp3, etc. If you serialize an object to xml it will have a header, if you call one of save methods in image class you will also have a file with a header, but the headers will be different. Ultimately an application will need to open the file, and the header will instruct the app how to handle the data the file contains.

    As for your text editor, 868D = 0x0364, so it looks like an Endian issue.
    kevin
    Last edited by kebo; Jan 27th, 2015 at 03:55 AM.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: How does VB.Net store variables in a binary file?

    It sounds like you are using a BinaryWriter to write the String to the file. However, if you are writing Object Types, then it also sounds like you are working with Option Strict turned Off, which is rarely a good idea.

    Are you employing the correct approach here? These days, people tend to work with databases or binary serialization when they need to store data of mixed Types. That said, there is nothing to stop you creating your own binary file format. You can add your own header fields to identify what the following piece of data might be. In the case of your image data (presumably as a byte array), you could add a header that identifies it as an image and specifies its length.


    As to your question: the string length header is 7 bit encoded. The documentation at https://msdn.microsoft.com/en-us/lib...vs.100%29.aspx says it is UTF-7 encoded, but that is wrong. See the community additions at the bottom of the page.

    Basically, the number is split into groups of 7 bits (padding with leading 0's if needed to get an exact multiple of 7). These groups are converted back into 8 bit bytes by prepending each group with a 1, except for the high order group, which is prepended with a 0. Since Windows stores numbers in Little Endian order, the order of the groups is then reversed when stored in the file.

    So, for a string of length 868 characters:

    Code:
    1101100100         = 868 = 0x364
    00001101100100     pad left to multiple of 7 bits
     0000110  1100100  split into groups of 7
    00000110 11100100  prepend 0 to first group
    		   and prepend 1 to all other groups
    
    11100100 00000110 rearrange in Little Endian order
    
    which is 0xE406
    See http://en.wikipedia.org/wiki/LEB128 for further explanation.

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    51

    Re: How does VB.Net store variables in a binary file?

    Thanks. I am using the zigzag data structure invented by Ted Nelson instead of a conventional relational database and using my own binary format to implement it. My software will include a view of the zigzag structure, a simple database for doing nature checklists and an innovative photo organizer tuned to the needs of the amateur naturalist. I am tired of using Windows Live Photo Gallery where I have to put number subscripts of every tag to put my images in taxonomy order, but everything else is worse. DigiKam is OK, but runs too slow on my machine.

    I will consider turning Option Strict on and see if I find a way to still use the object variable (likely with some conversions).

Posting Permissions

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



Click Here to Expand Forum to Full Width