By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,175 Members | 1,710 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,175 IT Pros & Developers. It's quick & easy.

Fixed sized output

P: n/a
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.
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Ellarco" <no****@eircom.net> wrote...
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
Jul 19 '05 #2

P: n/a

"Ellarco" <no****@eircom.net> wrote in message
news:%m*******************@news.indigo.ie...
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
Jul 19 '05 #3

P: n/a

"Ellarco" <no****@eircom.net> wrote in message
news:di*******************@news.indigo.ie...
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
Jul 19 '05 #4

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote...

"Ellarco" <no****@eircom.net> wrote in message
news:di*******************@news.indigo.ie...
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.


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
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.