Connecting Tech Pros Worldwide Forums | Help | Site Map

how to get the binary file ??

news.hku.hk
Guest
 
Posts: n/a
#1: Jul 22 '05
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::out);
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



Martijn Lievaart
Guest
 
Posts: n/a
#2: Jul 22 '05

re: how to get the binary file ??


On Sat, 10 Jan 2004 16:56:22 +0800, news.hku.hk wrote:
[color=blue]
> 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::out);
> 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"};[/color]

You probably want:

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

[color=blue]
>
> out << hex << buffer; // i think the problem is
> here[/color]

Try the write member of ostream:

out.write(buffer, sizeof(buffer);
[color=blue]
> 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[/color]

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

news.hku.hk
Guest
 
Posts: n/a
#3: Jul 22 '05

re: how to get the binary file ??


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" <m@remove.this.part.rtij.nl> wrote in message
news:pan.2004.01.10.12.54.06.362987@remove.this.pa rt.rtij.nl...[color=blue]
> On Sat, 10 Jan 2004 16:56:22 +0800, news.hku.hk wrote:
>[color=green]
> > i am writing a small program to get a binary file, but i really don't[/color][/color]
know[color=blue][color=green]
> > how to convert the strings in a buffer to the required binary bytes,[/color][/color]
most[color=blue][color=green]
> > 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::out);
> > 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"};[/color]
>
> You probably want:
>
> unsigned char buffer[]
> = { 0x7f 0x45 0x4c ... etc...
>
>[color=green]
> >
> > out << hex << buffer; // i think the problem is
> > here[/color]
>
> Try the write member of ostream:
>
> out.write(buffer, sizeof(buffer);
>[color=green]
> > 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[/color]
>
> 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
>[/color]


David Harmon
Guest
 
Posts: n/a
#4: Jul 22 '05

re: how to get the binary file ??


On Sun, 11 Jan 2004 12:05:43 +0800 in comp.lang.c++, "news.hku.hk"
<billychu@hkusua.hku.hk> was alleged to have written:[color=blue]
>also, the content of the buffer is given.........that means i can't change
>its content[/color]

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;

Karl Heinz Buchegger
Guest
 
Posts: n/a
#5: Jul 22 '05

re: how to get the binary file ??


"news.hku.hk" wrote:[color=blue]
>
> 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 ![/color]

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).

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Old Wolf
Guest
 
Posts: n/a
#6: Jul 22 '05

re: how to get the binary file ??


> >char buffer[50]
={"7f 45 4c 46 01 02 01 00 00 00 2f 75 73 72 2f 6c 12 13 7f 82"};[color=blue][color=green]
> >
> >also, the content of the buffer is given.........that means i can't change
> >its content[/color]
>
> But, if you really cannot change the buffer content,[/color]

If he can't change that line of code then he is in trouble, as
the content is more than 50 chars :)
[color=blue]
> 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;[/color]

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;

?
news.hku.hk
Guest
 
Posts: n/a
#7: Jul 22 '05

re: how to get the binary file ??


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


"Old Wolf" <oldwolf@inspire.net.nz> wrote in message
news:843a4f78.0401121945.714a6400@posting.google.c om...[color=blue][color=green][color=darkred]
> > >char buffer[50][/color][/color]
> ={"7f 45 4c 46 01 02 01 00 00 00 2f 75 73 72 2f 6c 12 13 7f 82"};[color=green][color=darkred]
> > >
> > >also, the content of the buffer is given.........that means i can't[/color][/color][/color]
change[color=blue][color=green][color=darkred]
> > >its content[/color]
> >
> > But, if you really cannot change the buffer content,[/color]
>
> If he can't change that line of code then he is in trouble, as
> the content is more than 50 chars :)
>[color=green]
> > 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;[/color]
>
> 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;
>
> ?[/color]


Closed Thread