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
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