![]() |
|
#1
|
|||
|
|||
One int to one char* for binary outputHi!
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:
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
|
|||
|
|||
Re: One int to one char* for binary outputQuote:
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
|
|||
|
|||
Re: One int to one char* for binary outputHi 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:
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:
it seems to work perfectly well. But still, I don't know what the problem with the other way is.. Cheers, Daniela |
|
#4
|
|||
|
|||
Re: One int to one char* for binary outputQuote:
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:
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
|
|||
|
|||
Re: One int to one char* for binary outputQuote:
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:
Quote:
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 GIDBlog
Observations of Iraq by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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