Portable 'byte' numeric type

Discussion in 'C++' started by Owen Jacobson, Jun 1, 2004.

  1. Salve.

    Does anyone have any suggestions for writing a portable 'byte' numeric
    type? I'm aware that ([un]signed) char is a numeric type and can be used
    as such, and I assume this is the fundamental building block for what I'm
    trying to do; however, I can't think of a way to typedef this that won't
    stomp on the definition of char on at least some platforms.

    Ideally, I'd like to be able to have the following:

    byte a = 0x20; // dec: 32, ascii: ' '
    unsigned char b = 'b';
    signed char c = 'c';

    std::cout << b << a << c << std::endl

    and get back
    b32c
    rather than
    b c
    or
    973298

    Is this even possible, portably?

    I considered, briefly, using a class:

    class byte {
    ... interface ...
    private:
    unsigned char mVal;
    };

    but I seem to recall that sizeof(byte) in this case is not guaranteed to
    be the same as sizeof(unsigned char) -- it is on my current compiler, but
    that's just a happy coincidence, right?

    The real problem I'm trying to solve here is buffering network I/O (which
    is non-portable, but someone else's problem) in a way that allows me to
    address arbitrary points in the message using pointer math; unsigned char
    * will work for this. However, I'd like to be able to print elements of
    the buffer, for debugging, without resorting to ugly hacks like

    // unsigned char *buffer;

    std::cout << ... << (0 + *buffer) << ... << std::endl;

    Any thoughts?

    Owen

    --
    Some say the Wired doesn't have political borders like the real world,
    but there are far too many nonsense-spouting anarchists or idiots who
    think that pranks are a revolution.
     
    Owen Jacobson, Jun 1, 2004
    #1
    1. Advertising

  2. Owen Jacobson

    Leor Zolman Guest

    On Tue, 01 Jun 2004 05:11:44 GMT, Owen Jacobson
    <> wrote:

    >Salve.
    >
    >Does anyone have any suggestions for writing a portable 'byte' numeric
    >type? I'm aware that ([un]signed) char is a numeric type and can be used
    >as such, and I assume this is the fundamental building block for what I'm
    >trying to do; however, I can't think of a way to typedef this that won't
    >stomp on the definition of char on at least some platforms.
    >
    >Ideally, I'd like to be able to have the following:
    >
    >byte a = 0x20; // dec: 32, ascii: ' '
    >unsigned char b = 'b';
    >signed char c = 'c';
    >
    >std::cout << b << a << c << std::endl
    >
    >and get back
    >b32c
    >rather than
    >b c
    >or
    >973298
    >
    >Is this even possible, portably?
    >
    >I considered, briefly, using a class:
    >
    >class byte {
    > ... interface ...
    >private:
    > unsigned char mVal;
    >};
    >
    >but I seem to recall that sizeof(byte) in this case is not guaranteed to
    >be the same as sizeof(unsigned char) -- it is on my current compiler, but
    >that's just a happy coincidence, right?


    If you don't have virtual functions, I'm not sure what could make your
    objects any bigger that char. But...

    >
    >The real problem I'm trying to solve here is buffering network I/O (which
    >is non-portable, but someone else's problem) in a way that allows me to
    >address arbitrary points in the message using pointer math; unsigned char
    >* will work for this. However, I'd like to be able to print elements of
    >the buffer, for debugging, without resorting to ugly hacks like
    >
    >// unsigned char *buffer;
    >
    >std::cout << ... << (0 + *buffer) << ... << std::endl;
    >
    >Any thoughts?


    If all you're actually concerned about is the appearance of the values when
    you print them, the alternative ugly hack of:

    std::cout << ... << static_cast<int>(*buffer) << ....

    or, the IMO least ugly one:

    std::cout << .. << int(*buffer) << ...

    seem to me to be the most straight-forward solutions.
    -leor

    >
    >Owen


    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: download BD Software's free STL Error Message Decryptor at:
    www.bdsoft.com/tools/stlfilt.html
     
    Leor Zolman, Jun 1, 2004
    #2
    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. Eli Bendersky
    Replies:
    1
    Views:
    1,206
    Mike Treseler
    Mar 1, 2006
  2. Replies:
    7
    Views:
    947
  3. Replies:
    5
    Views:
    981
    X-Centric
    Jun 30, 2005
  4. darrel
    Replies:
    4
    Views:
    868
    darrel
    Jul 19, 2007
  5. jobs

    int to numeric numeric(18,2) ?

    jobs, Jul 21, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    1,017
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Jul 22, 2007
Loading...

Share This Page