473,789 Members | 2,155 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

binary data conversion

Well, I'm stuck in legacy land and I need a helping hand.

We're trying to give some modern value-added functionality to
a circa-1985 fortran proggie.

The program produces a binary file, by itself no problem...
each record needs to be converted into std::vector<mys truct>
I'm having a helluva time with the binary-->(pod)datatyp e conversion.

Although I've referred to C++ containers, iostreams
are not used... FILE* only. Insert/extract operators are not an option.

Here's 3 records (broken into lines for easy reading)...

D5840000428F000 0140501000B0000 000000
993A000066C7000 0140501000F0000 000000
C0880000538D010 0140501001D0000 000000

Here's (pod) mystruct...

static struct IDX_RECORD /* length 18 bytes */
{
unsigned long respnum; // 4 bytes
unsigned long datfile_offset; // 4 bytes
unsigned char disposition; // 1 byte
unsigned char status; // 1 byte
unsigned char segment_svd; // 1 byte
unsigned char not_used; // 1 byte
unsigned short segment_dupr; // 2 bytes
unsigned long next_segment_of fset; // 4 bytes
};

Is this one of those times fscanf IS the "better" (whatever that means) way?

Assuming the powers have big-time aversion to "scan" functions,
what's an alternative?

Thank you for your thoughts,
Drake
Nov 14 '05 #1
7 1876
Drake wrote:
Well, I'm stuck in legacy land and I need a helping hand.

We're trying to give some modern value-added functionality to
a circa-1985 fortran proggie.

The program produces a binary file, by itself no problem...
each record needs to be converted into std::vector<mys truct>


Bummer. Ask C++ questions in a C++ newsgroup like news:comp.lang. c++
Nov 14 '05 #2

"Drake" <x@y.net> wrote in message
news:Xn******** *************** @207.217.125.20 4...
Well, I'm stuck in legacy land and I need a helping hand.

We're trying to give some modern value-added functionality to
a circa-1985 fortran proggie.

The program produces a binary file, by itself no problem...
each record needs to be converted into std::vector<mys truct>
I'm having a helluva time with the binary-->(pod)datatyp e conversion.

Although I've referred to C++ containers, iostreams
are not used... FILE* only. Insert/extract operators are not an option.

Here's 3 records (broken into lines for easy reading)...

D5840000428F000 0140501000B0000 000000
993A000066C7000 0140501000F0000 000000
C0880000538D010 0140501001D0000 000000

Here's (pod) mystruct...

static struct IDX_RECORD /* length 18 bytes */
{
unsigned long respnum; // 4 bytes
unsigned long datfile_offset; // 4 bytes
unsigned char disposition; // 1 byte
unsigned char status; // 1 byte
unsigned char segment_svd; // 1 byte
unsigned char not_used; // 1 byte
unsigned short segment_dupr; // 2 bytes
unsigned long next_segment_of fset; // 4 bytes
};
I'll assume those byte sizes in your comments refer
to your particular implementation. The language
doesn't require those sizes (but it does require
minimum sizes).

Is this one of those times fscanf IS the "better" (whatever that means) way?
Assuming the powers have big-time aversion to "scan" functions,
what's an alternative?


fscanf() is not what you need. It reads textual data.

Use fread(). And be sure to account for 'endianness'.

-Mike
Nov 14 '05 #3
Mac
On Sun, 11 Apr 2004 00:14:44 +0000, Drake wrote:
Well, I'm stuck in legacy land and I need a helping hand.

We're trying to give some modern value-added functionality to
a circa-1985 fortran proggie.

The program produces a binary file, by itself no problem...
each record needs to be converted into std::vector<mys truct>
I'm having a helluva time with the binary-->(pod)datatyp e conversion.

Although I've referred to C++ containers, iostreams
are not used... FILE* only. Insert/extract operators are not an option.

Here's 3 records (broken into lines for easy reading)...

D5840000428F000 0140501000B0000 000000
993A000066C7000 0140501000F0000 000000
C0880000538D010 0140501001D0000 000000
Is this a hex dump of the file, or is the file text... You said it was
binary earlier.

Here's (pod) mystruct...

static struct IDX_RECORD /* length 18 bytes */
{
unsigned long respnum; // 4 bytes
unsigned long datfile_offset; // 4 bytes
unsigned char disposition; // 1 byte
unsigned char status; // 1 byte
unsigned char segment_svd; // 1 byte
unsigned char not_used; // 1 byte
unsigned short segment_dupr; // 2 bytes
unsigned long next_segment_of fset; // 4 bytes
};

Is this one of those times fscanf IS the "better" (whatever that means) way?

Well, if the file is text, I think fscanf() is probably the best way, but
you can do whatever you want, including reading a character at a time with
getc() and not calling any other library functions. Another function to
consider is fgets() (again, assuming a text file.)

If the file is binary, which is what you said initially, I definitely
would not use fscanf(). I would use getc() or fread().
Assuming the powers have big-time aversion to "scan" functions,
what's an alternative?

What do you mean by "powers?" Is that short for "powers that be," meaning,
loosely, whoever is in charge?
Thank you for your thoughts,
Drake

I think you may want comp.lang.c++.

If you want a c solution, I suggest you rephrase to make that more clear,
removing all reference to c++, just to be safe, and repost.

If you want a c++ solution, post in comp.lang.c++, but read their faq
and/or lurk first to make sure you don't annoy anyone.

--Mac
Nov 14 '05 #4
Martin Ambuhl <ma*****@earthl ink.net> wrote in news:c5a47k$2m9 rj0$3@ID-
227552.news.uni-berlin.de:
Drake wrote:
Well, I'm stuck in legacy land and I need a helping hand.

We're trying to give some modern value-added functionality to
a circa-1985 fortran proggie.

The program produces a binary file, by itself no problem...
each record needs to be converted into std::vector<mys truct>


Bummer. Ask C++ questions in a C++ newsgroup like news:comp.lang. c++

A few lines later the post tried and obviously failed in avoiding this
answer.

<< Although I've referred to C++ containers, iostreams
<< are not used... FILE* only. Insert/extract operators are not an
<< option.

Anyway std::vector isn't the problem. It's the conversion.
Drake
Nov 14 '05 #5
"Drake" <x@.y.net> wrote in message
news:Ju******** *********@newsr ead2.news.pas.e arthlink.net...
Martin Ambuhl <ma*****@earthl ink.net> wrote in news:c5a47k$2m9 rj0$3@ID-
227552.news.uni-berlin.de:
Drake wrote:
Well, I'm stuck in legacy land and I need a helping hand.

We're trying to give some modern value-added functionality to
a circa-1985 fortran proggie.

The program produces a binary file, by itself no problem...
each record needs to be converted into std::vector<mys truct>


Bummer. Ask C++ questions in a C++ newsgroup like news:comp.lang. c++

A few lines later the post tried and obviously failed in avoiding this
answer.

<< Although I've referred to C++ containers, iostreams
<< are not used... FILE* only. Insert/extract operators are not an
<< option.

Anyway std::vector isn't the problem. It's the conversion.
Drake


Tastefully dispatched. ;-)

Nov 14 '05 #6

"Drake" <x@y.net> wrote in message

Here's 3 records (broken into lines for easy reading)...

D5840000428F000 0140501000B0000 000000
993A000066C7000 0140501000F0000 000000
C0880000538D010 0140501001D0000 000000

Here's (pod) mystruct...

static struct IDX_RECORD /* length 18 bytes */
{
unsigned long respnum; // 4 bytes
unsigned long datfile_offset; // 4 bytes
unsigned char disposition; // 1 byte
unsigned char status; // 1 byte
unsigned char segment_svd; // 1 byte
unsigned char not_used; // 1 byte
unsigned short segment_dupr; // 2 bytes
unsigned long next_segment_of fset; // 4 bytes
};

Assuming the powers have big-time aversion to "scan" functions,
what's an alternative?

As someone else has pointed out, you don't say whether your example is a
hexdump or a text file. fscanf() is useful only for text files, but of
limited utlity if numerics are not separated by delimiters.

What you need are the functions

unsigned long read32us(FILE *fp);
unsigned short read16us(FILE *fp);

If you want to use C++ you can be ugly and replace the FILE * with a stream,
or be object-oriented and derive a class from iostream with the functions
added.
In either case, build the read32() function on top of fgetc() / character
extraction.
If you are using C++, throw exceptions in the case of premature EOF.
Nov 14 '05 #7
Drake wrote:
Well, I'm stuck in legacy land and I need a helping hand.

We're trying to give some modern value-added functionality to
a circa-1985 fortran proggie.

The program produces a binary file, by itself no problem...
each record needs to be converted into std::vector<mys truct>
I'm having a helluva time with the binary-->(pod)datatyp e conversion.

Although I've referred to C++ containers, iostreams
are not used... FILE* only. Insert/extract operators are not an option.

Here's 3 records (broken into lines for easy reading)...

D5840000428F000 0140501000B0000 000000
993A000066C7000 0140501000F0000 000000
C0880000538D010 0140501001D0000 000000

Here's (pod) mystruct...

static struct IDX_RECORD /* length 18 bytes */
{
unsigned long respnum; // 4 bytes
unsigned long datfile_offset; // 4 bytes
unsigned char disposition; // 1 byte
unsigned char status; // 1 byte
unsigned char segment_svd; // 1 byte
unsigned char not_used; // 1 byte
unsigned short segment_dupr; // 2 bytes
unsigned long next_segment_of fset; // 4 bytes
};

Is this one of those times fscanf IS the "better" (whatever that means) way?

Assuming the powers have big-time aversion to "scan" functions,
what's an alternative?

Thank you for your thoughts,
Drake


If your file is ASCII encoded hex file (a.k.a. Hex dump),
then you may want to read each line using fgets(), then
fill in your data structure by converting the ASCII values
into native formats. This is probably the faster method
than fscanf.

If the file is actually binary, you may want to use the
fread function and read a block into a buffer then load
the fields of your structure from the buffer.

In no case do you want to load your structure directly
from the file or memory. The simple rule is that the
size of a structure may not be equal to the size of its
members. The compiler is allowed to add "padding" bytes
between members. This is what kills most programs that
attempt to map a structure directly to an input stream.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #8

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

Similar topics

7
4221
by: laclac01 | last post by:
So I am converting some matlab code to C++. I am stuck at one part of the code. The matlab code uses fread() to read in to a vector a file. It's a binary file. The vector is made up of floats, which in matlab is 32 bits. How do I get this binary file in to floats in c++? I try reading the file using the ifstream>>myFloat. But nothing ever goes in to the float. So the closest I have come is having 4 unsigned char store the binary data....
103
48764
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it says about it. What the heck does the binary flag mean? -- If our hypothesis is about anything and not about some one or more particular things, then our deductions constitute mathematics. Thus mathematics may be defined as the subject in...
2
3638
by: geskerrett | last post by:
In the '80's, Microsoft had a proprietary binary structure to handle floating point numbers, In a previous thread, Bengt Richter posted some example code in how to convert these to python floats; http://groups.google.com/group/comp.lang.python/browse_thread/thread/42150ccc20a1d8d5/4aadc71be8aeddbe#4aadc71be8aeddbe I copied this code and modified it slightly, however, you will notice that for one of the examples, the conversion isn't...
9
2283
by: bowsayge | last post by:
Inspired by fb, Bowsayge decided to write a decimal integer to binary string converter. Perhaps some of the experienced C programmers here can critique it. It allocates probably way too much memory, but it should certainly handle 64-bit cpus :) #include <stdio.h> #include <stdlib.h> char * to_binary (unsigned long value) {
26
4320
by: Patient Guy | last post by:
Has anyone written code that successfully manipulates binary file data using Javascript? It might---and in the case of doing I/O, will---make use of browser- specific functions (ActiveX/COM with Internet Explorer, XPCOM/XPConnect with Mozilla/Firefox). I am writing client-side code that will generate binary data for producing a GIF file through an OBJECT element. (The GIF is an image of a line and points on a two-axis plot.)
10
3856
by: Wrecked | last post by:
Could someone suggest a method to convert a Binary data to an ASCII data, with very less or no increase in the memory . The problem basically is there is an excrypted message which needs to be transmitted via SMS (mobiles). SMS supports only transmitting of printable char ie ASCII characters. I do know methords where in after conversion the size becomes twice the original data. Does some one know any better, optimal way to do the...
7
7029
by: smith4894 | last post by:
Hello all, I'm working on writing my own streambuf classes (to use in my custom ostream/isteam classes that will handle reading/writing data to a mmap'd file). When reading from the mmap file, I essentially have a char buffer in my streambuf class, that I'm registering with setp(). on an overflow() call, I simply copy the contents of the buffer into the mmap'd file via memcpy().
1
2758
TMS
by: TMS | last post by:
I'm trying to write an address book that is based on a binary tree. I'm devloping in Visual C++ (I blew up my Ubuntu with the new dist, so no EMACS), starting with the basics: #ifndef binarySearchTree_h #define binarySearchTree_h #include <string> #include <iostream> using namespace std;
7
19234
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
12
3714
by: fermineutron | last post by:
I am trying to write a function which will convert a large ascii number, say 100 ascii digits, to its binary representation. It seems that evey algorithm I am trying to think of is backwards. Normaly in pen and paper ascii to binary conversion one would start by subtracting largest power of 2 that can be subtracted from the given number and work from left to right filling memory up with 1s and 0s. In my case I cant really do that b/c the...
0
9502
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10187
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10132
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
9007
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, and deployment—without 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...
0
6753
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
5412
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
5544
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4084
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
2
3687
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.