C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

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

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
jiing
Guest





PostPosted: Mon Apr 18, 2005 7:21 am    Post subject: How to transfer the values in a binary file to decimal ? Reply with quote



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."< }else{
cout<<"File is open."< }

char ch ;
int buffer[8192];

while( !fin.eof() ){
fin.get(ch);
cout< this?
}
fin.close();

Back to top
Karl Heinz Buchegger
Guest





PostPosted: Mon Apr 18, 2005 8:00 am    Post subject: Re: How to transfer the values in a binary file to decimal ? Reply with quote



jiing wrote:
Quote:

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."< }else{
cout<<"File is open."< }

char ch ;
int buffer[8192];

while( !fin.eof() ){
fin.get(ch);
cout< 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.

Quote:
}
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;
}


--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
jiing
Guest





PostPosted: Mon Apr 18, 2005 8:30 am    Post subject: Re: How to transfer the values in a binary file to decimal ? Reply with quote



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< }

for(int i=0; i sum = sum + buffer[i];
}

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.

Back to top
cafeeee
Guest





PostPosted: Mon Apr 18, 2005 10:43 am    Post subject: Re: How to transfer the values in a binary file to decimal ? Reply with quote

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 element of "buffer"
cout< }


for(int i=0; i sum = sum + buffer[i];
}

Back to top
jiing
Guest





PostPosted: Mon Apr 18, 2005 11:23 am    Post subject: Re: How to transfer the values in a binary file to decimal ? Reply with quote

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."< }
char ch ;
vector 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< }

Back to top
Karl Heinz Buchegger
Guest





PostPosted: Mon Apr 18, 2005 12:04 pm    Post subject: Re: How to transfer the values in a binary file to decimal ? Reply with quote

jiing wrote:
Quote:

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:

Quote:
}
char ch ;

Make that: unsigned char ch;

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

--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
Rolf Magnus
Guest





PostPosted: Mon Apr 18, 2005 12:08 pm    Post subject: Re: How to transfer the values in a binary file to decimal ? Reply with quote

jiing wrote:

Quote:
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.


Back to top
jiing
Guest





PostPosted: Wed Apr 20, 2005 8:33 am    Post subject: Re: How to transfer the values in a binary file to decimal ? Reply with quote

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::get(unsigned
char&)'

Back to top
Kurious
Guest





PostPosted: Tue May 17, 2005 1:31 pm    Post subject: Re: How to transfer the values in a binary file to decimal ? Reply with quote

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/resources/docs/modules/pod/perlembed.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:......PerlThingPerlThingDlg.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
Back to top
Julián Albo
Guest





PostPosted: Tue May 17, 2005 1:46 pm    Post subject: Re: How to transfer the values in a binary file to decimal ? Reply with quote

Kurious wrote:

Quote:
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.

--
Salu2

Back to top
Kurious
Guest





PostPosted: Wed May 18, 2005 1:14 am    Post subject: Re: How to transfer the values in a binary file to decimal ? Reply with quote

On Tue, 17 May 2005 15:46:56 +0200, Julián Albo <JULIANALBO (AT) terra (DOT) es>
wrote:
Quote:

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.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.