How to transfer the values in a binary file to decimal ?

J

jiing

I am reading a binary file *.dat, (8-bit unsigned char)
Now I want to transfer the value in it as decimal, how can I do ?
Thanks in advance.

ifstream fin("usb1_8192_out.dat", ios::binary | ios::in );
if (!fin.is_open() ){
cout<<"File is not open."<<endl;
}else{
cout<<"File is open."<<endl;
}

char ch ;
int buffer[8192];

while( !fin.eof() ){
fin.get(ch);
cout<<ch; //here is output binary, how to deal with
this?
}
fin.close();
 
K

Karl Heinz Buchegger

jiing said:
I am reading a binary file *.dat, (8-bit unsigned char)
Now I want to transfer the value in it as decimal, how can I do ?
Thanks in advance.

ifstream fin("usb1_8192_out.dat", ios::binary | ios::in );
if (!fin.is_open() ){
cout<<"File is not open."<<endl;
}else{
cout<<"File is open."<<endl;
}

char ch ;
int buffer[8192];

while( !fin.eof() ){
fin.get(ch);
cout<<ch; //here is output binary, how to deal with
this?

The simplest way is:

cout << (int)ch;


A char is nothing else then a small integer. Only during input and output
a char is treated differently. While integer types are feed through a function
which comes up with a character representation for that inetger, a char is treated
as a character.
}
fin.close();

Also note: Your reading loop is wrong. eof() is ment to be used after a read loop
has terminated to figure out why it has terminated. The correct way in C++ is
to use the return value of all input functions to figure out if the read operation
worked or failed. eof() returns true only after you tried *and failed* to read past
the end of file.

while( fin.get( ch ) ) {
// do something with the read thing
}

if( !fin.eof() ) { // not read till eof(), something else
// must have happend
cout << "Error during read" << endl;
// process that error;
}
 
J

jiing

Karl, thank you for your kind help.

But I still have some problem

int i = 0;
char ch ;
vector<int> buffer;
long sum = 0;

while( fin.get( ch ) ) {
buffer.push_back( (unsigned int)ch );
i++;
}
fin.close();
// calculate mean
for(i=1; i<=10; i++){
cout<<buffer<<endl;
}

for(int i=0; i<buffer.size(); i++){
sum = sum + buffer;
}

The output is: -121 117 103 -80 -93 57 120
-36 108 47
But when I see the binary file, they are
7F 87 75 67 B0 A3 39 78 DC 6C
g除9x釿

They real value I wanted should be
127 135 117 103 176 163 57 120 220 108

Can anyone teach me. I can not figure out what mistack I made.
Thank you.
 
C

cafeeee

int i = 0;
char ch ;
vector<int> buffer;
long sum = 0;


while( fin.get( ch ) ) {
buffer.push_back( (int)ch ); // change "unsigned int" to "int"
i++;
}
fin.close();
// calculate mean
for(i=0; i<buffer.size(); i++){ // you missed the first
element of "buffer"
cout<<buffer<<endl;
}


for(int i=0; i<buffer.size(); i++){
sum = sum + buffer;
}
 
J

jiing

cafeeee, thank you,

But I have one question
After revising, the first 10 result still have some negative
so I add the with 256 if them are negative, and get what I want
But I am not very sure why I can not cast them as unsigned int
can anyone teach me? thank you.

ifstream fin("usb1_8192k_out.dat", ios::binary | ios::in );
if (!fin.is_open() ){
cout<<"input file is not open."<<endl;
}
char ch ;
vector<int> buffer;
while( fin.get( ch ) ){
int temp;
(int(ch) < 0)?(temp = int(ch)+256):(temp = int(ch) ); // I added
this line
buffer.push_back(temp);
}
fin.close();

// calculate mean
for(int i=0; i<10; i++){
cout<<buffer<<endl;
}
 
K

Karl Heinz Buchegger

jiing said:
cafeeee, thank you,

But I have one question
After revising, the first 10 result still have some negative
so I add the with 256 if them are negative, and get what I want
But I am not very sure why I can not cast them as unsigned int
can anyone teach me?

The problem is the 'char'.
Having learned that 'char' is nothing else then a
small integer, answer a question: Is char signed or unsigned?

And the answer is: It is left to the implementation if it treats
a char as signed or unsigned.

But you can force the compiler to do what you want:
}
char ch ;

Make that: unsigned char ch;

And now the compiler treats ch in arithmetic as if it had no
sign (always positive).
 
R

Rolf Magnus

jiing said:
cafeeee, thank you,

But I have one question
After revising, the first 10 result still have some negative
so I add the with 256 if them are negative, and get what I want
But I am not very sure why I can not cast them as unsigned int
can anyone teach me? thank you.

You are using char, which is probably signed on your implementation. For the
typical 8bit 2's complement char, that means that its range goes from -128
to +127. The values 0 to 127 are the same for the signed and unsigned
versions, 128 is equivalent to -127, 129 to -126 and so on.
What you need to do is use unsigned char instead of char.
 
J

jiing

Thanks for your answering.

but the file get function seems can not deal with unsigned char

when I revise the code as follows:

unsigned char ch ;
vector<int> buffer;
while( fin.get( ch ) ){

The compiler tells me
36 C:\usb_check_lost_2.cpp no matching function for call to
`std::basic_ifstream<char, std::char_traits<char> >::get(unsigned
char&)'
 
K

Kurious

Hi guys,

I'm crossing my fingers and hoping someone out there is familiar with
embedding perl into C++...

I'm trying to do that using the following code:

char *embedding[] = { "", "drawGraph.pl", "0" };
my_perl = perl_alloc();
perl_construct( my_perl );
perl_parse(my_perl, NULL, 3, embedding, NULL);
perl_run(my_perl);
perl_call_argv("drawGraph", G_DISCARD | G_NOARGS, embedding);

Now this calls the subroutine drawGraph in the file drawGraph.pl ok if
there are no modules being included. However as soon as I include the
line
use GD::Graph::bars;
it crashes. I'm assuming it has something to do with what is described
on this page:
http://www.monster-submit.com/resou...erlembed.html#Using_Perl_modules_which_themse
however I have been unable to implement that solution. After adding
the code thus:

#ifdef __cplusplus
# define EXTERN_C extern "C"
#else
# define EXTERN_C extern
#endif

static void xs_init _((void));

EXTERN_C void boot_DynaLoader _((CV* cv));
EXTERN_C void boot_Socket _((CV* cv));

EXTERN_C void
xs_init()
{
char *file = __FILE__;
/* DynaLoader is a special case */
newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
newXS("Socket::bootstrap", boot_Socket, file);
}

When compiling I get the error:

linkage specification contradicts earlier specification for 'xs_init'
D:\......\PerlThing\PerlThingDlg.cpp(187) : see declaration of
'xs_init'

followed by two:
error C2664: 'Perl_newXS' : cannot convert parameter 3 from 'void
(struct cv *)' to 'void (__cdecl *)(struct interpreter *,struct cv *)'
None of the functions with this name in scope match the target
type

I have been unable to find where to go from here. I don't really
understand what's going on, and I haven't been able to find a resource
that explains this. What's going on here, and what am I missing? Is
there an easier way around this? Is there something I should have read
up on before posting?

Thanks guys,

Hyde
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Kurious said:
I'm crossing my fingers and hoping someone out there is familiar with
embedding perl into C++...

I have a program that does it... but not exactly, it uses a mdoule written
in C to glue perl embedded and C++ parts. I done it that way because of
some compiler, operating system and perl version used needs.

You can download the source from:

http://www.arrakis.es/~ninsesabe/qtre/ (the page is in spanish only, sorry,
"Descarga" is Download).

You can see in the Makefile how to generate the xinit code.

For more information, better ask in some perl newsgroup or mailing list.
 
K

Kurious

I have a program that does it... but not exactly, it uses a mdoule written
in C to glue perl embedded and C++ parts. I done it that way because of
some compiler, operating system and perl version used needs.

You can download the source from:

http://www.arrakis.es/~ninsesabe/qtre/ (the page is in spanish only, sorry,
"Descarga" is Download).

You can see in the Makefile how to generate the xinit code.

For more information, better ask in some perl newsgroup or mailing list.

Thanks for that, I'll check it out. I didn't mean to post this as a
reply to this message. I'll repost it as a general one.
 

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