473,698 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3463
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_bac k( (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_bac k( (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):(t emp = int(ch) ); // I added
this line
buffer.push_bac k(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_lo st_2.cpp no matching function for call to
`std::basic_ifs tream<char, std::char_trait s<char> >::get(unsign ed
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_p erl, NULL, 3, embedding, NULL);
perl_run(my_per l);
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("DynaLoad er::boot_DynaLo ader", boot_DynaLoader , file);
newXS("Socket:: bootstrap", boot_Socket, file);
}

When compiling I get the error:

linkage specification contradicts earlier specification for 'xs_init'
D:\......\PerlT hing\PerlThingD lg.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

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

Similar topics

7
4086
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? For instance, floats can distinguish 0.000001 and 0.000002. If I started with 0.000001 and kept adding 0.000001 until I hit some maximum value (FLT_MAX?) and kept a counter of how many times I added 0.000001, would I have a count of how many...
11
6632
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 transfer data across.On the serve I am using Socket 2 API (recv function to read bytes)and not using ..NET. I use FileStream to open the file on the pocket pc, then associate a BinaryReader object with the stream and call ReadBytes to read all the...
1
1643
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 = 4; Power = -1; Power--) { Remainder = Decimal - (pow(2,Power));
11
2444
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 data in a specific file by specifying the file name, start date/time and end date/time. The files are in binary format, to conserve space on the server (as well as to increase processing time). The data in each file can be quite large, covering...
2
4191
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 object attempts a connection, a new instance of a Client object is created (in the server's clients hashtable). The client object on the server (the client solution itself has same communication setup only with the code in the main form) handles...
7
19216
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 00000000000000001111111111111111 11111111111111111111111111111111 00000000000000000000000000000000 11111111111111110000000000000000
2
4495
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
7202
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 fine, but I am having some trouble with manipulating my hex values. Seems to me that there are two ways to store hex values: 1. as literal hex - 0x55aa 2. as a string - "\x55aa"
3
10883
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 SimpleXMLRPCServer import SimpleXMLRPCServer import xmlrpclib
0
8673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9156
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8892
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7716
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4365
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.