0D after 0A in hex when writing a binary file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Romain

    0D after 0A in hex when writing a binary file

    Hello,

    I am writing out a binary file. I figured that the number "10" is
    automaticaly converted to "OD OA" instead of "OD". "OD" and "OA" are
    line feed and carriage return.

    I know it does that if the file is opened in something else than in
    binary mode. But in my example below, the file is really opened in
    binary.

    I am compiling the file with VC++.NET


    I am using 010 Editor to edit the binary file.

    Thanks for your help,

    Ctest2App::Ctes t2App()
    {
    long i;
    long Size=10;
    LONG len=1;

    ofstream *BinFile; // The acq file streams
    PUCHAR *buf=new PUCHAR[Size];
    BinFile = new ofstream("out.b in");


    for (i =0; i < Size; i++) {
    buf[i] = new UCHAR[len];
    buf[i][0]=10;
    }
    for (i =0; i < Size; i++) BinFile->write((char *) buf[i], len);
    }

  • Victor Bazarov

    #2
    Re: 0D after 0A in hex when writing a binary file

    Romain wrote:[color=blue]
    > I am writing out a binary file. I figured that the number "10" is
    > automaticaly converted to "OD OA" instead of "OD". "OD" and "OA" are
    > line feed and carriage return.[/color]

    Actually, reverse. "0D" and "0A" are carriage return and line feed
    (and those are zeros and not Os in the numbers).
    [color=blue]
    > I know it does that if the file is opened in something else than in
    > binary mode. But in my example below, the file is really opened in
    > binary.[/color]

    Nope.
    [color=blue]
    >
    > I am compiling the file with VC++.NET
    >
    >
    > I am using 010 Editor to edit the binary file.
    >
    > Thanks for your help,
    >
    > Ctest2App::Ctes t2App()
    > {
    > long i;
    > long Size=10;
    > LONG len=1;
    >
    > ofstream *BinFile; // The acq file streams
    > PUCHAR *buf=new PUCHAR[Size];
    > BinFile = new ofstream("out.b in");[/color]

    What makes you think this is opening as binary? The fact that you made
    the name to contain letters 'b', 'i', and 'n', is definitely not enough.
    Do

    BinFile = new ofstream("out.b in", ios::binary);

    [color=blue]
    >
    >
    > for (i =0; i < Size; i++) {
    > buf[i] = new UCHAR[len];
    > buf[i][0]=10;
    > }
    > for (i =0; i < Size; i++) BinFile->write((char *) buf[i], len);
    > }
    >[/color]

    Victor

    Comment

    • Howard

      #3
      Re: 0D after 0A in hex when writing a binary file


      "Romain" <romain.bonne@v oila.fr> wrote in message
      news:1105378860 .597866.223660@ f14g2000cwb.goo glegroups.com.. .[color=blue]
      > Hello,
      >
      > I am writing out a binary file. I figured that the number "10" is
      > automaticaly converted to "OD OA" instead of "OD". "OD" and "OA" are
      > line feed and carriage return.
      >
      > I know it does that if the file is opened in something else than in
      > binary mode. But in my example below, the file is really opened in
      > binary.[/color]

      Really? Isn't the default mode text? The code below doesn't specify binary
      anywhere.
      [color=blue]
      >
      > I am compiling the file with VC++.NET
      >
      >
      > I am using 010 Editor to edit the binary file.
      >
      > Thanks for your help,
      >
      > Ctest2App::Ctes t2App()
      > {
      > long i;
      > long Size=10;
      > LONG len=1;
      >
      > ofstream *BinFile; // The acq file streams
      > PUCHAR *buf=new PUCHAR[Size];
      > BinFile = new ofstream("out.b in");[/color]

      Why are you using a pointer here? And where's the specifier saying it
      should be binary? I think this should be:

      ofstream BinFile("out.bi n",ios_base::bi nary);
      [color=blue]
      >
      >
      > for (i =0; i < Size; i++) {
      > buf[i] = new UCHAR[len];
      > buf[i][0]=10;
      > }
      > for (i =0; i < Size; i++) BinFile->write((char *) buf[i], len);
      > }
      >[/color]

      (Any reason you're dynamically allocating these buffers? Just curious. If
      you need dynamic sizing, you might consider using the string class instead
      of dynamic arrays.)

      -Howard



      Comment

      • Mike Wahler

        #4
        Re: 0D after 0A in hex when writing a binary file


        "Romain" <romain.bonne@v oila.fr> wrote in message
        news:1105378860 .597866.223660@ f14g2000cwb.goo glegroups.com.. .[color=blue]
        > Hello,
        >
        > I am writing out a binary file. I figured that the number "10" is
        > automaticaly converted to "OD OA" instead of "OD". "OD" and "OA" are
        > line feed and carriage return.[/color]

        The number 10 is the encoding for the 'line feed' character in
        ASCII. But note that C++ does not mandate a particular character
        set.

        Also, such conversions when doing file i/o are platform-specific.
        You're describing what happens for MSDOS and MS-Windows, but
        C++ can be run with many other operating systems as well, some
        of which have different translations, or none at all.

        Also, your "OD OA" above is using an upper case letter 'O',
        but those should be the digit zero (0).
        [color=blue]
        >
        > I know it does that if the file is opened in something else than in
        > binary mode.[/color]

        On some platforms. Also note that in C++ there is only one
        'something else' besides 'binary mode', which is 'text mode'.
        Streams opened in 'text mode' will possibly induce these
        translations, *for some platforms*, not all.
        [color=blue]
        >But in my example below, the file is really opened in
        > binary.[/color]

        Um, no it is not. The default mode for a stream is text.
        [color=blue]
        >
        > I am compiling the file with VC++.NET
        >
        >
        > I am using 010 Editor to edit the binary file.
        >
        > Thanks for your help,
        >
        > Ctest2App::Ctes t2App()
        > {
        > long i;
        > long Size=10;
        > LONG len=1;
        >
        > ofstream *BinFile; // The acq file streams[/color]

        This is not a stream object, but a pointer to one.
        Why are you using a pointer?
        [color=blue]
        > PUCHAR *buf=new PUCHAR[Size];
        > BinFile = new ofstream("out.b in");[/color]

        This opens the stream in text mode, which will cause translations
        on some platforms. BTW why are you dynamically allocating the
        stream instead of simply defining one, e.g.

        ofstream ofs("out.bin");

        You also failed to check whether the open succeeded or failed.
        If it failed, any subsequent attempts to use the stream will
        certainly fail as well.
        [color=blue]
        >
        >
        > for (i =0; i < Size; i++) {
        > buf[i] = new UCHAR[len];[/color]

        I don't understand this need you seem to have to dynamically
        allocate things. This only makes the code unnecessarily
        complex and harder to maintain.
        [color=blue]
        > buf[i][0]=10;
        > }
        > for (i =0; i < Size; i++) BinFile->write((char *) buf[i], len);[/color]

        [color=blue]
        > }
        >[/color]

        Which C++ book(s) are you reading?

        -Mike


        Comment

        Working...