Writing bytes to a file

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

    Writing bytes to a file

    Hi, I want to dump 1500 bytes at a time to a file as part of the
    logger i am writing for my driver.
    However i want to the bytes to appear as text but as hex value not
    characters.
    I also need to separate each block of 1500 bytes by a new line and a
    sequence number which identifies each block.

    The following sample code gives me text output

    static std::ofstream FileOutput("dat a.out",std::ios ::out);
    static std::ostringstr eam os;
    unsigned char Data[1500];

    os << SeqNo << std::endl;

    for (int i = 0; i < BufferLength; ++i)
    {
    os << Data [i];

    }

    os << std::endl;

    How do i format Data to appear as hex values

    Fileoutput << os.str();
  • tech

    #2
    Re: Writing bytes to a file

    On Mar 12, 10:57 am, tech <naumansulai... @googlemail.com wrote:
    Hi, I want to dump 1500 bytes at a time to a file as part of the
    logger i am writing for my driver.
    However i want to the bytes to appear as text but as hex value not
    characters.
    I also need to separate each block of 1500 bytes by a new line and a
    sequence number which identifies each block.
    >
    The following sample code gives me text output
    >
    static std::ofstream FileOutput("dat a.out",std::ios ::out);
    static std::ostringstr eam os;
    unsigned char Data[1500];
    >
         os << SeqNo << std::endl;
    >
         for (int i = 0; i < BufferLength; ++i)
          {
            os <<  Data [i];
    >
          }
    >
       os << std::endl;
    >
    How do i format Data to appear as hex values
    >
    Fileoutput << os.str();

    ok no problem worked how to do just cast the char to an int and

    os << std::hex << (int)Data[i];

    Comment

    • Juha Nieminen

      #3
      Re: Writing bytes to a file

      tech wrote:
      static std::ofstream FileOutput("dat a.out",std::ios ::out);
      static std::ostringstr eam os;
      unsigned char Data[1500];
      >
      os << SeqNo << std::endl;
      >
      for (int i = 0; i < BufferLength; ++i)
      {
      os << Data [i];
      >
      }
      >
      os << std::endl;
      >
      How do i format Data to appear as hex values
      >
      Fileoutput << os.str();
      Why use a stringstream when you can write directly to the output stream?

      This may be do what you want:

      FileOutput.fill ('0');
      for (int i = 0; i < BufferLength; ++i)
      {
      FileOutput << std::setw(2) << std::setbase(16 ) << Data [i];
      }

      Comment

      • tech

        #4
        Re: Writing bytes to a file

        On Mar 12, 12:02 pm, Juha Nieminen <nos...@thanks. invalidwrote:
        tech wrote:
        static std::ofstream FileOutput("dat a.out",std::ios ::out);
        static std::ostringstr eam os;
        unsigned char Data[1500];
        >
             os << SeqNo << std::endl;
        >
             for (int i = 0; i < BufferLength; ++i)
              {
           os <<  Data [i];
        >
              }
        >
           os << std::endl;
        >
        How do i format Data to appear as hex values
        >
        Fileoutput << os.str();
        >
          Why use a stringstream when you can write directly to the output stream?
        >
          This may be do what you want:
        >
          FileOutput.fill ('0');
          for (int i = 0; i < BufferLength; ++i)
          {
              FileOutput << std::setw(2) << std::setbase(16 ) << Data [i];
          }- Hide quoted text -
        >
        - Show quoted text -
        Hi, that didn't work because the Data is unsigned char type and the
        file is opened
        in text mode then its not being formatted to hex for some reason.

        My method
        os << std::hex << (int)Data[i];

        works but the conversion to int is taking too long so its affecting
        the logging. Your'e right
        about using the file stream direct though.

        So what i want to do is write a unsigned char as a hex value to a file
        opened in text mode
        without converting to an int

        Comment

        • Juha Nieminen

          #5
          Re: Writing bytes to a file

          tech wrote:
          works but the conversion to int is taking too long so its affecting
          the logging.
          Are you sure it's the conversion to int that is taking too long, and
          not something else?

          Maybe try using std::fprintf() instead of std::ofstream.

          Comment

          Working...