Writing bytes to a file

T

tech

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::eek:fstream FileOutput("data.out",std::ios::eek:ut);
static std::eek:stringstream os;
unsigned char Data[1500];

os << SeqNo << std::endl;

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

}

os << std::endl;

How do i format Data to appear as hex values

Fileoutput << os.str();
 
T

tech

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::eek:fstream FileOutput("data.out",std::ios::eek:ut);
static std::eek:stringstream os;
unsigned char Data[1500];

     os << SeqNo << std::endl;

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

      }

   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;
 
J

Juha Nieminen

tech said:
static std::eek:fstream FileOutput("data.out",std::ios::eek:ut);
static std::eek:stringstream os;
unsigned char Data[1500];

os << SeqNo << std::endl;

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

}

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 ;
}
 
T

tech

tech said:
static std::eek:fstream FileOutput("data.out",std::ios::eek:ut);
static std::eek:stringstream os;
unsigned char Data[1500];
     os << SeqNo << std::endl;
     for (int i = 0; i < BufferLength; ++i)
      {
   os <<  Data ;

      }
   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 ;
  }- 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;

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
 
J

Juha Nieminen

tech said:
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::eek:fstream.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top