I'm new to C++ and I'm trying to write a file that contains binary information. In exactness, I'm learning to use C++ to write out .swf files. In a .swf file, the fourth byte is always the version number. However, not the ASCII character of the version number, for example, I've got to have it be 00001000 for 8, not 00111000 for a decimal of 56 to give an ASCII character of 8. No matter what I try, it's always just getting written out as ASCII 8. I'm using fstream by the way.
I tried using:
int version = 8;
mySWF << dec << version;
and:
int version = 8;
mySWF << hex << version;
as well as:
mySWF << 0x08;
With what knowledge I have on the subject (limited) any of these should have worked, but the end product always has an ASCII 8 where binary 8 should be. Any suggestions?
6 1765
A lot of this depends on how you define "Binary File".
All files are stored in binary (everything the average computer does is in binary). A text editor, interprets nimary into text, so while internally 8 is represented as something like 00001000 (optionally add more 0's first), it is displayed as 8.
If you want to store a file to display binary values, you have to interpret the values into a binary list yourself. One way to do this (for a character) would be: -
const char BYTESIZE = 8;
-
char myVal = 8;
-
int output = 0;
-
-
for(int i=0; i<BYTESIZE; i++)
-
{
-
output = myVal % 2;
-
cout << output;
-
myVal = myVal/2;
-
}
-
-
This code: -
const char BYTESIZE = 8;
-
char myVal = 5;
-
int output = 0;
-
-
for(int i=0; i<BYTESIZE; i++)
-
{
-
output = myVal % 2;
-
cout << output;
-
myVal = myVal/2;
-
}
-
puts out the binary in reverse order. You need to: -
const char BYTESIZE = 8;
-
char myVal = 5;
-
char output;
-
-
for(int pos = 128; pos; pos /= 2)
-
{
-
output = (myVal/pos) % 2 + 48;
-
cout << output;
-
}
-
cout << endl;
-
What I need is not to display binary as ASCII characters, but to write the byte of the file at the bit level. When you open the created file in a binary editor, the first four bytes should be:
01000110 01010111 01010011 00001000
The first three are easy, because they are the ASCII characters "F" "W" and "S". The fourth is what's giving me trouble, because it is not the ASCII, it's the binary of 8, which I don't know how to get C++ to write. Writing the binary 00001000 OR writing the hex of 0x08 should work, but I don't know how to do that in C++. No matter what I do, I end up with 00111000, which is 56, for the ASCII of 8, which won't work for a .swf file.
I'm not new to computer science, only C++, I know exactly what the binary of the file needs to read, I just don't know how to get C++ to write it at the binary level ; )
What I need is not to display binary as ASCII characters, but to write the byte of the file at the bit level. When you open the created file in a binary editor, the first four bytes should be:
01000110 01010111 01010011 00001000
The first three are easy, because they are the ASCII characters "F" "W" and "S". The fourth is what's giving me trouble, because it is not the ASCII, it's the binary of 8, which I don't know how to get C++ to write. Writing the binary 00001000 OR writing the hex of 0x08 should work, but I don't know how to do that in C++. No matter what I do, I end up with 00111000, which is 56, for the ASCII of 8, which won't work for a .swf file.
I'm not new to computer science, only C++, I know exactly what the binary of the file needs to read, I just don't know how to get C++ to write it at the binary level ; )
U can mask that bit.
00111000
&
00001000
--------------
00001000
Savage
You write the 8 in binary as follows: -
ofstream out("C:\\scratch\\instructor\\bindata.txt", ios::binary);
-
unsigned char data = 8;
-
out.put(data);
-
out.close();
-
//
-
//Read it back in
-
ifstream in("C:\\scratch\\instructor\\bindata.txt");
-
unsigned char xx = 0;
-
xx = in.get();
-
//Look at it in the debugger
-
ahhh, that's exactly what I needed to know, thank you very much.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: yaipa |
last post by:
What would be the common sense way of finding a binary pattern in a
..bin file, say some 200 bytes, and replacing it with an updated pattern
of the same length at the same offset?
Also, the...
|
by: Christian Stigen Larsen |
last post by:
A signed int reserves one bit to signify whether a number is positive or
negative. In light of this, a colleague asked me whether there existed an
int in C++ that was -0, a zero with the negative...
|
by: Tron Thomas |
last post by:
What does binary mode for an ofstream object do anyway? Despite which
mode the stream uses, operator << writes numeric value as their ASCII
representation.
I read on the Internet that it is...
|
by: Steven T. Hatton |
last post by:
§27.4.2.1.4 Type ios_base::openmode
Says this about the std::ios::binary openmode flag:
*binary*: perform input and output in binary mode (as opposed to text mode)
And that is basically _all_ it...
|
by: Ching-Lung |
last post by:
Hi all,
I try to create a tool to check the delta (diff) of 2
binaries and create the delta binary. I use binary
formatter (serialization) to create the delta binary. It
works fine but the...
|
by: John Dann |
last post by:
I'm trying to read some binary data from a file created by another
program. I know the binary file format but can't change or control the
format. The binary data is organised such that it should...
|
by: vim |
last post by:
hello everybody
Plz tell the differance between binary file and ascii
file...............
Thanks
in advance
vim
|
by: bwv539 |
last post by:
I have to output data into a binary file, that will contain data
coming from a four channel measurement instrument.
Since those data have to be read from another C program somewhere
else, the...
|
by: rory |
last post by:
I can't seem to append a string to the end of a binary file. I'm using
the following code:
fstream outFile("test.exe", ios::in | ios::out | ios::binary |
ios::ate | ios::app)...
|
by: Erwin Moller |
last post by:
Why is a binary file executable? Is any binary file executable? Is
only binary file executable? Are all executable files binary? What is
the connection between the attribute of binary and that of...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
| |