CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 23 of 23
  1. #16
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Little/Big Endian test code in machine and file

    If you have files that can be used by both big and little endian systems, one way is not to use binary numbers but instead have them in the file as they are displayed as chars. Then programs can extract the numbers as chars and convert to the correct internal representation as needed.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  2. #17
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Little/Big Endian test code in machine and file

    As 2Kaud says, storing everything as strings (or use something like xml) is an easy method for avoiding the issue entirely.

    If you are writing doubles.
    then...
    1) you may need to deal with endian-ness of the data
    2) you may also need to deal with different internal binary representations of the double type
    3) note that endian ness is only half the problem. there are also systems where bits in a byte are stored in memory in reverse. so you may not only have to deal with byte ordering, but also bit-ordering.

    again, this all depends on what types of systems the C++ needs to compile for (truely portable code takes quite a bit of effort)
    and/or what systems your data can come from.

    Note that the common approach on the internet is to assume all data is big-endian (this is true for IP packets etc.) so you need to swap in intel machines.
    If you have the luxury of deciding the file format... THen the "easy" way is to avoid the issue entirely and use a text format, while typically a bit slower, and harder to achieve in fixed record sizes, there are advantages in the fact the files remain "user-editable" and typically are easier to access from other programs.

  3. #18
    Join Date
    May 2009
    Posts
    29

    Re: Little/Big Endian test code in machine and file

    Hi again,

    I totally agree with you both. Remaining with text data is safer. I'll implement my files in that way - at least is universal.

    The problem might be the size of the files (will a microsoft database be better? I wonder...) ...I'll start thinking about that instead of waisting my time with "endian" issues... :/

    Kind regards,

    JKepler

  4. #19
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Little/Big Endian test code in machine and file

    a database typically takes care of endianness at the driver level.
    but it may be difficult to transfer a full database between systems regularly (that's not what databases typically do)

  5. #20
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Little/Big Endian test code in machine and file

    Between which systems are you going to share these files and what is the volume of data shared?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #21
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Little/Big Endian test code in machine and file

    Quote Originally Posted by OReubens View Post
    a database typically takes care of endianness at the driver level.
    but it may be difficult to transfer a full database between systems regularly (that's not what databases typically do)
    Well, at least SQLite states:

    The database file format is cross-platform - you can freely copy a database between 32-bit and 64-bit systems or between big-endian and little-endian architectures.
    Best regards,
    Igor

  7. #22
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Little/Big Endian test code in machine and file

    CFD codes write out in binary, since writing "text"s would be considerably slower and increase the size of data written. They usually take care of it one of two ways:

    1) similar to "netcdf classic" : always write out big endian, and the library takes care of any conversians during the read/write process

    2) always write out native, and the library take care of any needed conversions when reading.

    CFD codes often have an to read/write a text formatted file also. But I have never seen anyone need to do that.

  8. #23
    Join Date
    May 2009
    Posts
    29

    Re: Little/Big Endian test code in machine and file

    Actually, I've heard about SQLite a long time ago. It's interesting that point.

    But I've got something else in mind....

    Either way, I'll explore it.

    Cheers,

    JKepler

Page 2 of 2 FirstFirst 12

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

Featured