how to get the binary file ??

N

news.hku.hk

i am writing a small program to get a binary file, but i really don't know
how to convert the strings in a buffer to the required binary bytes, most
probably i can't read each bytes in buffer separately.........

Code:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
using namespace std;

int main() {
ofstream out;
out.open("test.bin", ios::binary|ios::eek:ut);
if (!out) {
cout << "Cannot open test.bin for output";
return 1;
}

char buffer[50]
={"7f 45 4c 46 01 02 01 00 00 00 2f 75 73 72 2f 6c 12 13 7f 82"};

out << hex << buffer; // i think the problem is
here

out.close();
cout << "test.bin created" << endl;
return 0;
}



The expected screen output of od -t x1 test.bin is:
ELF /usr/l

i really has no idea about it.......please help
 
M

Martijn Lievaart

i am writing a small program to get a binary file, but i really don't know
how to convert the strings in a buffer to the required binary bytes, most
probably i can't read each bytes in buffer separately.........

Code:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
using namespace std;

int main() {
ofstream out;
out.open("test.bin", ios::binary|ios::eek:ut);
if (!out) {
cout << "Cannot open test.bin for output";
return 1;
}

char buffer[50]
={"7f 45 4c 46 01 02 01 00 00 00 2f 75 73 72 2f 6c 12 13 7f 82"};

You probably want:

unsigned char buffer[]
= { 0x7f 0x45 0x4c ... etc...

out << hex << buffer; // i think the problem is
here

Try the write member of ostream:

out.write(buffer, sizeof(buffer);
out.close();
cout << "test.bin created" << endl;
return 0;
}



The expected screen output of od -t x1 test.bin is:
ELF /usr/l

i really has no idea about it.......please help

You are mixing two things.

1) You have a textual representation of the hex contents of the file. This
is something very different from an actual hex representation.

2) Operator<< and the manipulator hex are designed for textual I/O. The
write member (and istream::read) are designed for binary I/O.

HTH,
M4
 
N

news.hku.hk

Sorry, just now i typed my question wrongly,
the output of od -t x1 myfile.bin should be:

0000000 7f 45 4c 46 01 02 01 00 00 00 2f 75 73 72 2f 6c
0000020 12 13 7f 82

also, the content of the buffer is given.........that means i can't change
its content
when i open the test.bin with notepad, the output would be in unreadable
form
also, i've tried the write member of out stream, but it simply copies what i
have in the buffer to test.bin which isn't wanted. Could you help me again,
thanks a lot !
Billy

Martijn Lievaart said:
i am writing a small program to get a binary file, but i really don't know
how to convert the strings in a buffer to the required binary bytes, most
probably i can't read each bytes in buffer separately.........

Code:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
using namespace std;

int main() {
ofstream out;
out.open("test.bin", ios::binary|ios::eek:ut);
if (!out) {
cout << "Cannot open test.bin for output";
return 1;
}

char buffer[50]
={"7f 45 4c 46 01 02 01 00 00 00 2f 75 73 72 2f 6c 12 13 7f 82"};

You probably want:

unsigned char buffer[]
= { 0x7f 0x45 0x4c ... etc...

out << hex << buffer; // i think the problem is
here

Try the write member of ostream:

out.write(buffer, sizeof(buffer);
out.close();
cout << "test.bin created" << endl;
return 0;
}



The expected screen output of od -t x1 test.bin is:
ELF /usr/l

i really has no idea about it.......please help

You are mixing two things.

1) You have a textual representation of the hex contents of the file. This
is something very different from an actual hex representation.

2) Operator<< and the manipulator hex are designed for textual I/O. The
write member (and istream::read) are designed for binary I/O.

HTH,
M4
 
D

David Harmon

also, the content of the buffer is given.........that means i can't change
its content

I guess that Martijn's answer is really the right one, and that you
should change the buffer content.

But, if you really cannot change the buffer content, then you will have
to treat it as a string of formatted input. Something roughly like:

std::istringstream in(buffer);
int value;
while (in >> hex >> value)
out << (char)value;
 
K

Karl Heinz Buchegger

news.hku.hk said:
Sorry, just now i typed my question wrongly,
the output of od -t x1 myfile.bin should be:

0000000 7f 45 4c 46 01 02 01 00 00 00 2f 75 73 72 2f 6c
0000020 12 13 7f 82

also, the content of the buffer is given.........that means i can't change
its content
when i open the test.bin with notepad, the output would be in unreadable
form
also, i've tried the write member of out stream, but it simply copies what i
have in the buffer to test.bin which isn't wanted. Could you help me again,
thanks a lot !

Start with breaking the string into pieces.
You can't do what you want to do as long as you have


"7f 45 4c 46 01 02 01 00 00 00 2f 75 73 72 2f 6c 12 13 7f 82"


You need to break this string into parts:

"7f"
"45"
"4c"
"46"
...

once that part is done, you create an unsigned char from the textual
representation:

"7f" -> 7 * 16 + 15 -> 127 'writing' 127 to the output stream gives a hex value of 0x7f
"45" -> 4 * 16 + 5 -> 69 'writing' 69 to the output stream gives a hex value of 0x45
"4c" -> 4 * 16 + 12 -> 76 'writing' 76 to the output stream gives a hex value of 0x4c
"46" -> 4 * 16 + 6 -> 70 'writing' 70 -"- 0x46
...

For this you need to seperate each string into the individual characters and recode them into
numbers:

'0' -> 0
'1' -> 1
'2' -> 2
...
'9' -> 9
'a' -> 10
'A' -> 11
'b' -> 12
'B' -> 12
...
'f' -> 15
'F' -> 15

(Note: some of the stream functionalities in conjunction with a string stream could
be handy. If you don't want to use this, then a function doing the conversion is
an easy exercise to do).
 
O

Old Wolf

char buffer[50]
={"7f 45 4c 46 01 02 01 00 00 00 2f 75 73 72 2f 6c 12 13 7f 82"};
But, if you really cannot change the buffer content,

If he can't change that line of code then he is in trouble, as
the content is more than 50 chars :)
then you will have
to treat it as a string of formatted input. Something roughly like:

std::istringstream in(buffer);
int value;
while (in >> hex >> value)
out << (char)value;

This involves sending the hex manipulator to the stream every time,
would it be advisable to instead go:

std::stringstream ss(buffer);
int value;
ss << std::hex;
while (ss >> value)
out << (char)value;

?
 
N

news.hku.hk

Thanks a lot ! Old Wolf, your suggestion works !
i also appreciate the detailed explanation from Karl Heinz Buchegger.
I love this newsgroup !


Old Wolf said:
char buffer[50] ={"7f 45 4c 46 01 02 01 00 00 00 2f 75 73 72 2f 6c 12 13 7f 82"};

also, the content of the buffer is given.........that means i can't change
its content

But, if you really cannot change the buffer content,

If he can't change that line of code then he is in trouble, as
the content is more than 50 chars :)
then you will have
to treat it as a string of formatted input. Something roughly like:

std::istringstream in(buffer);
int value;
while (in >> hex >> value)
out << (char)value;

This involves sending the hex manipulator to the stream every time,
would it be advisable to instead go:

std::stringstream ss(buffer);
int value;
ss << std::hex;
while (ss >> value)
out << (char)value;

?
 

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top