473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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();

Jul 23 '05 #1
10 3442
jiing wrote:

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;
}
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2
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[i]<<endl;
}

for(int i=0; i<buffer.size(); 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.

Jul 23 '05 #3
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[i]<<endl;
}
for(int i=0; i<buffer.size(); i++){
sum = sum + buffer[i];
}

Jul 23 '05 #4
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[i]<<endl;
}

Jul 23 '05 #5
jiing wrote:

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

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #6
jiing wrote:
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.

Jul 23 '05 #7
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&)'

Jul 23 '05 #8
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/resour...s_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
Jul 23 '05 #9
Kurious wrote:
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
Jul 23 '05 #10
On Tue, 17 May 2005 15:46:56 +0200, Julián Albo <JU********@terra.es>
wrote:

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.
Jul 23 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Johnathan Doe | last post by:
I can google search to find the range of values that can be represented in a float by reading up on the IEEE std, but is that the same as how many distinct values that can go in a float type? ...
11
by: Abhishek | last post by:
I have a problem transfering files using sockets from pocket pc(.net compact c#) to desktop(not using .net just mfc and sockets 2 API). The socket communication is not a issue and I am able to...
1
by: lmh86 | last post by:
Should the following algorithm produce the binary equivalent of an inputed decimal value? ---------- cout << "Decimal value: "; cin >> Decimal; Binary = 0; Remainder = Decimal; for(Power =...
11
by: E.T. Grey | last post by:
Hi, I have an interesting problem. I have a (LARGE) set of historical data that I want to keep on a central server, as several separate files. I want a client process to be able to request the...
2
by: Bonzol | last post by:
vb.net 2003 Windows application We have a Client/Server system set up by communicating through a TCPClient object. The server loops continuously using a tcplistener device and each time a client...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
2
by: Tukeind | last post by:
Hello, I am receiving the following error: error C2065: 'to_binary' : undeclared identifier while running the code below. If anyone can help I'll appreciate it? Thank you, Tukeind
1
by: Stephen Cattaneo | last post by:
Hi all, I am relatively new to socket programming. I am attempting to use raw sockets to spoof my IP address. From what I can tell I will have to build from the Ethernet layer on up. This is...
3
by: celery6541 | last post by:
Hi all, I am trying to transfer files via xmlrpc. I am following the example found here: http://docs.python.org/library/xmlrpclib.html under the binary objects section Server Code from...
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.