Fixed sized output

E

Ellarco

How can I perform output (file output for the monent, possibly socket in the
future) in such a way that output produced on different compiler/machines
pairs is indistinguishable? Specifically my concern is over different byte
sizes. If I want to output say 32 bits how do I do that? If one machine has
8-bit chars then 4 of those will do but what happens when the code is run on
say a 9-bit char machine? 4 chars will output 36-bits then. There is some
way to achieve this right? The reason - the output is to be read in Java so
I want to output fixed size data so that I can simply say readInt() (32
bits), readLong() (64 bits) etc. I want binary due to the amount of data and
of course time effeciency is a concern.

Please and thank you.
 
V

Victor Bazarov

Ellarco said:
How can I perform output (file output for the monent, possibly socket in the
future) in such a way that output produced on different compiler/machines
pairs is indistinguishable? Specifically my concern is over different byte
sizes. If I want to output say 32 bits how do I do that? If one machine has
8-bit chars then 4 of those will do but what happens when the code is run on
say a 9-bit char machine? 4 chars will output 36-bits then. There is some
way to achieve this right? The reason - the output is to be read in Java so
I want to output fixed size data so that I can simply say readInt() (32
bits), readLong() (64 bits) etc. I want binary due to the amount of data and
of course time effeciency is a concern.

For the binary output to look exactly the same way on several
platforms, you will have to write platform-specific code to convert
your internal data into some form that when written looks the same.

In general binary exchange between systems should be avoided for
this particular reason: incompatibility at the binary level. IOW,
if you want to do it right, use text.

Victor
 
J

John Harrison

Ellarco said:
How can I perform output (file output for the monent, possibly socket in the
future) in such a way that output produced on different compiler/machines
pairs is indistinguishable? Specifically my concern is over different byte
sizes. If I want to output say 32 bits how do I do that? If one machine has
8-bit chars then 4 of those will do but what happens when the code is run on
say a 9-bit char machine?

I think the possibility of a 9 bit char is so remote that you need not worry
about it.
4 chars will output 36-bits then. There is some
way to achieve this right? The reason - the output is to be read in Java so
I want to output fixed size data so that I can simply say readInt() (32
bits), readLong() (64 bits) etc. I want binary due to the amount of data and
of course time effeciency is a concern.

I doubt that an 8-bit char machine and a 9-bit char machine cound even
communicate with each other without some software between to handle the
transition from 8 bit bytes to 9 bit bytes. What the right thing for you to
do would depend on how that software handled the transition, not something
under your control.
Please and thank you.

john
 
J

John Harrison

Ellarco said:
Okay, youre not telling me anything I want to hear. Is this not a reasonably
common problem? Are there no libraries designed for fixed size, network
order binary output?

Speculate on this if you have a moment. Ill be using the Java Native
Interface (JNI) headers. The JNI defines types that are
<tongue-in-cheek>guaranteed</tongue-in-cheek> to be the same size as Java
primitives. So a jbyte is 8-bits, a jint is 32-bits, etc. For example, on my
platform (Wintel) jbyte is typedef'd to a signed char. Do you reckon that if
a reputable body (e.g. Sun) has written JNI headers for a particular
platform, that executing code such as

#include <jni.h>
...
jint ji = 10;
...
myOstream << ji;
...

will output 32-bits regardless of that platforms specific/crazy byte sizes?
Would it even be possible to define a jbyte (8-bits) on a 9 bit byte machine
(I pick 9-bits cos its awkward, not because its likely -- I want my code
very portable, with the aid of the Autotools).

Care to speculate?

Thanks to both for replies.

El.

20 years in the business, I've never come accross a machine that didn't have
8 bit bytes. I think you are just going to have to accept that were you to
come across such a machine your code would not be portable.

john
 
V

Victor Bazarov

John Harrison said:
on that

20 years in the business, I've never come accross a machine that didn't have
8 bit bytes. I think you are just going to have to accept that were you to
come across such a machine your code would not be portable.

I think that since Ellarco's problem is stemming from Java, and
since Java is a platform, the problem is platform-specific and
cannot be answered in terms of standard C++. Just MHO.

Victor
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top