Fixed sized output

Discussion in 'C++' started by Ellarco, Jul 8, 2003.

  1. Ellarco

    Ellarco Guest

    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.
     
    Ellarco, Jul 8, 2003
    #1
    1. Advertising

  2. "Ellarco" <> 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
     
    Victor Bazarov, Jul 8, 2003
    #2
    1. Advertising

  3. "Ellarco" <> wrote in message
    news:%mHOa.22087$...
    > 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
     
    John Harrison, Jul 9, 2003
    #3
  4. "Ellarco" <> wrote in message
    news:di1Pa.22254$...
    > 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
     
    John Harrison, Jul 10, 2003
    #4
  5. "John Harrison" <> wrote...
    >
    > "Ellarco" <> wrote in message
    > news:di1Pa.22254$...
    > > 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
     
    Victor Bazarov, Jul 10, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sanborne

    variable sized port map

    sanborne, Mar 18, 2006, in forum: VHDL
    Replies:
    6
    Views:
    2,945
    Mike Treseler
    Mar 26, 2006
  2. Edward

    Dynamically sized tabular display

    Edward, Feb 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    399
    Wardeaux
    Feb 4, 2004
  3. thechaosengine

    Oddly sized sized password textbox

    thechaosengine, Sep 15, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    590
    David Hearn
    Sep 15, 2005
  4. johnp
    Replies:
    4
    Views:
    3,680
    Toby Inkster
    May 23, 2005
  5. Justin Barrett

    Fixed sized datatypes.

    Justin Barrett, Dec 28, 2004, in forum: C++
    Replies:
    10
    Views:
    668
    Pete Becker
    Dec 29, 2004
Loading...

Share This Page