C++ io-stream how to write a real hex file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Markus Hämmerli

    C++ io-stream how to write a real hex file


    I ll try to write a file with a iostram. It is ok for ASCII but don't work
    for hex .

    So when i send a "01" I get ASCII 31 but Iwant a real 01 in the file.



    Thanks Markus m.haemmerli@sol net.ch

    This ist my code so fare:

    std::ofstream fileout(_T("C:\ \COMbin.hex"),s td::ios::out
    |std::ios::trun c|std::ios::bin ary);

    // fileout.setf(st d::ios::showbas e |std::ios::hex) ;

    int i=0x01;

    fileout<< i;



  • John Harrison

    #2
    Re: C++ io-stream how to write a real hex file


    "Markus Hämmerli" <m.haemmerli@so lnet.ch> wrote in message
    news:3f54e954$0 $20058$fb624d75 @newsspool.soln et.ch...[color=blue]
    >
    > I ll try to write a file with a iostram. It is ok for ASCII but don't[/color]
    work[color=blue]
    > for hex .
    >
    > So when i send a "01" I get ASCII 31 but Iwant a real 01 in the file.
    >
    >
    >
    > Thanks Markus m.haemmerli@sol net.ch
    >
    > This ist my code so fare:
    >
    > std::ofstream fileout(_T("C:\ \COMbin.hex"),s td::ios::out
    > |std::ios::trun c|std::ios::bin ary);
    >
    > // fileout.setf(st d::ios::showbas e |std::ios::hex) ;
    >
    > int i=0x01;
    >
    > fileout<< i;
    >[/color]

    Use write for binary output

    fileout.write(( char*)&i, sizeof i);

    Also use read for binary input. << is for text only.

    john


    Comment

    • Rolf Magnus

      #3
      Re: C++ io-stream how to write a real hex file

      Markus Hämmerli wrote:
      [color=blue]
      >
      > I ll try to write a file with a iostram. It is ok for ASCII but don't
      > work for hex .[/color]

      Well, operator<< is for formatted text output. So it will convert
      everything you have to text and write that text.
      If you want to write binary data (I assume that's what you mean by
      "hex"), you should read the faq at

      Look especially at the chapter about serialization.


      Comment

      • Xenos

        #4
        Re: C++ io-stream how to write a real hex file

        Don't use the stream operators, you will get ASCII. Use the read/write
        methods.

        "Markus Hämmerli" <m.haemmerli@so lnet.ch> wrote in message
        news:3f54e954$0 $20058$fb624d75 @newsspool.soln et.ch...[color=blue]
        >
        > I ll try to write a file with a iostram. It is ok for ASCII but don't[/color]
        work[color=blue]
        > for hex .
        >
        > So when i send a "01" I get ASCII 31 but Iwant a real 01 in the file.
        >
        >
        >
        > Thanks Markus m.haemmerli@sol net.ch
        >
        > This ist my code so fare:
        >
        > std::ofstream fileout(_T("C:\ \COMbin.hex"),s td::ios::out
        > |std::ios::trun c|std::ios::bin ary);
        >
        > // fileout.setf(st d::ios::showbas e |std::ios::hex) ;
        >
        > int i=0x01;
        >
        > fileout<< i;
        >
        >
        >[/color]


        Comment

        • Kevin Goodsell

          #5
          Re: C++ io-stream how to write a real hex file

          Xenos wrote:
          [color=blue]
          > Don't use the stream operators, you will get ASCII. Use the read/write
          > methods.[/color]

          Please don't top-post. Re-read section 5 of the FAQ for posting guidelines.



          Your assumption that the implementation uses the ASCII character set is
          unfounded.

          -Kevin



          --
          My email address is valid, but changes periodically.
          To contact me please use the address from a recent posting.

          Comment

          Working...