Boost logo

Boost :

From: Thomas Matelich (sosedada_at_[hidden])
Date: 2001-04-17 12:13:12


Daryle Walker wrote:

> Several people have had problems with the augmented CRC tests, while I
> haven't. I realized that the CRC algorithms assume that they get numbers
> most-significant bit first. For most raw data, you don't care about endian
> issues, but the augmented CRC tests read their own CRC after the raw data,
> causing a problem if the data is not in big-endian order. I suspect that
> the users reporting problems are using little-endian computers, while I have
> a big-endian computer. What can I do to solve this problem?

Sorry, if this is the obvious answer and you wanted more. You have 2 problems,
knowing what platform you're on and doing something about it.
1) Here is what I use in my code, the only important part is the
initialization of course.
namespace XPlat //short for CrossPlatform
{
//btw: BIG_ENDIAN_ORDER -> UNIX hardware, LITTLE_ENDIAN_ORDER -> x86 hardware
enum ByteOrdering { BIG_ENDIAN_ORDER, LITTLE_ENDIAN_ORDER };

class EndianNess
{
public:
    operator ByteOrdering() const
    {
        return byte_ordering;
    } //implicit conversion

    friend const EndianNess& Omega();
private:
    EndianNess()
    {
        //checks the first byte for a one
        //I have verified this to be correct on Windows and HPUX
        //method gleaned from many c.l.c++.m postings
        unsigned int n = 1;
        char* c;
        c = (char*)&n;
        byte_ordering = (!*c) ? BIG_ENDIAN_ORDER : LITTLE_ENDIAN_ORDER;
    }

    ByteOrdering byte_ordering;
};

//sorry about the name, Omega is the end -> Endian, get it?
inline const EndianNess& Omega()
{
    static EndianNess _omega;
    return _omega;
}

2) You have to either swap your data or swap your algorithm. Sorry to say, I
haven't looked at your code, so I don't know how applicable this is, but I use
std::reverse on native integral types like so:
int val;
if(i_need_to_swap)
{
    char* p = (char*)&val;
    std::reverse(p, p+sizeof(int));
}
//use val

But from what I've seen in your postings about speed, I would probably swap the
algorithm.

--
Thomas O Matelich
Senior Software Designer
Zetec, Inc.
sosedada_at_[hidden]
tmatelich_at_[hidden]

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk