GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Nov-2007, 07:23
c4a c4a is offline
New Member
 
Join Date: Nov 2007
Posts: 3
c4a is on a distinguished road

One int to one char* for binary output


Hi!

Still a beginner, I am trying to write binary output in a certain pattern. I have strings and integers to put into the file.

For this I am using a string as buffer, where I just append the data. I assume to save the most storage, I have to put one integer into one byte. So I am using a chars as containers.

This was already solved with java-code. Now I am trying to do the same with c++.

Below an example, how I have implemented this:

CPP / C++ / C Code:

   string str_buffer = ""; 

   string version = "10";
   string creator_name = "xy";
   string data_name = "Data";
   string cc = "DE";
  
   int foo= 12;
   int faa = 52;
   //...(some other integers)...

   str_buffer.push_back(creator_name.size());
   str_buffer += creator_name;
   // ...(same appends for other strings) ...

   int onebyte   = 0x000000ff;
   int twobyte   = 0x0000ff00;
   int threebyte = 0x00ff0000;
   int fourbyte  = 0xff000000;


   char outbyte = foo & onebyte;
   str_buffer.push_back(outbyte);

   outbyte = (foo & twobyte) >> 8;
   str_buffer.push_back(outbyte);

   outbyte = (foo & threebyte) >> 16;
   str_buffer.push_back(outbyte);

   outbyte = (foo & fourbyte) >> 24;
   str_buffer.push_back(outbyte);

   ofstream outputFile("test.bin",ofstream::binary);

   char* c_buffer;

   c_buffer = new char[str_buffer.size()];
   strcpy(c_buffer, str_buffer.c_str());
   outputFile.write (c_buffer, str_buffer.size());

   outputFile.close();


I get actually output for this, the strings seem to be ok, but I think the way I tried to solve the "one integer for one byte"- problem doesn't work.

What am I doing wrong, here?

Thanks for any help!
  #2  
Old 16-Nov-2007, 12:51
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: One int to one char* for binary output


Quote:
Originally Posted by c4a
H...the way I tried to solve the "one integer for one byte"- problem doesn't work....

A char is an integer data type.

If your ints are within the range -128 through +127, then converting between the ints and (signed) chars is lossless. That is: A simple assignment statement will convert one to the other, and you can get back the original value by another assignment statement.

If your ints are within the range 0 through +255, then converting between the ints and unsigned chars is lossless. That is: A simple assignment statement will convert one the other, and you can get back the original value by another assignment statement.

Perhaps you can show how you tested your scheme and came up with results that were not what you expected.

Regards,

Dave
  #3  
Old 20-Nov-2007, 02:57
c4a c4a is offline
New Member
 
Join Date: Nov 2007
Posts: 3
c4a is on a distinguished road

Re: One int to one char* for binary output


Hi Dave,
thanks for the answer. I solved it! :-)

Of course, you are right with the limited size of the char.
But with my implementation I am only looking byte-wise at the integer, I am throwing in. So I'll get my outcome as byte size - fine for my char. :-)

So the real flaw here was lying in the writing of the string, in which I have stuffed all chars.

CPP / C++ / C Code:
   ofstream outputFile("test.bin",ofstream::binary);

   char* c_buffer;

   c_buffer = new char[str_buffer.size()];
   strcpy(c_buffer, str_buffer.c_str());
   outputFile.write (c_buffer, str_buffer.size());


I don't actually know, why this is happening, but with this I got the weird output. A lot of "cd"s instead of real values, different from each other.

Instead with using just

CPP / C++ / C Code:
outputFile << str_buffer;

it seems to work perfectly well.

But still, I don't know what the problem with the other way is..

Cheers,
Daniela
  #4  
Old 20-Nov-2007, 07:51
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: One int to one char* for binary output


Quote:
Originally Posted by c4a

But with my implementation I am only looking byte-wise at the integer,
So the real flaw here was lying in the writing of the string, in which I have stuffed all chars.

Show us exactly what is in the "string". How did you build the string.

In other words show us exactly what you ran that you didn't understand (the entire program). Tell us exactly what you got and what you expected.

Regards,

Dave

Footnote:

CPP / C++ / C Code:
    strcpy(c_buffer, str_buffer.c_str());

1. This copies all of the bytes in the c_str() and also the terminating zero byte, so you should have allocate one more byte for c_buffer.

2. If your "string" has a zero byte in it, nothing after that will be copied by this statement. Using c_str() or any of the C-style string functions (strcpy, strcat, strlen, etc.) for a "string" that contains arbitrary integer values (like zero, for example) is a bug waiting to happen.
  #5  
Old 23-Nov-2007, 08:24
c4a c4a is offline
New Member
 
Join Date: Nov 2007
Posts: 3
c4a is on a distinguished road
Thumbs up

Re: One int to one char* for binary output


Quote:
Originally Posted by davekw7x
Show us exactly what is in the "string". How did you build the string.
In other words show us exactly what you ran that you didn't understand (the entire program). Tell us exactly what you got and what you expected.

Just as summary (all solved):
As you can see from my original post, I casted bytes of integers to chars and put these together with some other strings into another string.

Like this:
CPP / C++ / C Code:

   string str_buffer = "";

   //int := 1Byte:
     str_buffer.push_back(creator_name.size());
   
  // strings are just appended: 
    str_buffer += creator_name;
   
  // integer > 1Byte - using masks for only 1 Byte output:
   //used as masks: 
     int onebyte   = 0x000000ff;
     int twobyte   = 0x0000ff00;
     int threebyte = 0x00ff0000;
     int fourbyte  = 0xff000000;
  
    //masking and casting with this only one byte into char
    //effect: integer will be "cut into four bytes" and later 
    //with when interpreting file put together, again.
   
    char outbyte = foo & onebyte;
     str_buffer.push_back(outbyte);

     outbyte = (foo & twobyte) >> 8;
     str_buffer.push_back(outbyte);

     outbyte = (foo & threebyte) >> 16;
     str_buffer.push_back(outbyte);

     outbyte = (foo & fourbyte) >> 24;
     str_buffer.push_back(outbyte);


Quote:
Originally Posted by davekw7x
CPP / C++ / C Code:
    strcpy(c_buffer, str_buffer.c_str());

2. If your "string" has a zero byte in it, nothing after that will be copied by this statement. Using c_str() or any of the C-style string functions (strcpy, strcat, strlen, etc.) for a "string" that contains arbitrary integer values (like zero, for example) is a bug waiting to happen.

Thanks a lot for this clear explanation! :-)


I finally managed to get my binary ofstream right.
Sorry, for this delayed answer, but there was another stupid bug somewhere else within my code, which had produced some nonsense in my output, too.


Cheers,
Daniela
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bit stuffing agnostos C Programming Language 17 12-Sep-2006 20:10
editbox output problem FireFox8173 MS Visual C++ / MFC Forum 1 01-Apr-2005 14:15
using vector or array oshiotse C++ Forum 4 16-Apr-2004 10:59
urgent: problem + output filters jack Apache Web Server Forum 0 04-Feb-2004 22:32
articles -> output Allowee Learning Journal by J de Silva 1 05-Jun-2003 19:47

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:19.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.