Portable 'byte' numeric type

O

Owen Jacobson

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
 
L

Leor Zolman

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
 

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

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top