2
4

4 回答 4

5

Perhaps a little off topic, but I would personally use Boost.Format :

boost::format fmt("%02X-%02X-%02X-%02X-%02X-%02X");
for (int i = 0; i != 6; ++i)
    fmt % static_cast<unsigned int>(MACData[i]);
std::cout << fmt << std::endl;
于 2011-01-03T19:55:27.987 回答
4

You could do it like this (without changing the existing design of the app (I guess you can not, otherwise you would probably do it :)))

void printMacToStream(std::ostream& os, unsigned char MACData[])
{
    // Possibly add length assertion
    char oldFill = os.fill('0');

    os << std::setw(2) << std::hex << static_cast<unsigned int>(MACData[0]);
    for (uint i = 1; i < 6; ++i) {
        os << '-' << std::setw(2) << std::hex << static_cast<unsigned int>(MACData[i]);
    }

    os.fill(oldFill);

    // Possibly add:
    // os << std::endl;
}

Usage:

std::stringstream ss;
printMacToStream(ss, arrayWIthMACData);

Update: HEX format :)

于 2011-01-03T19:44:42.487 回答
1
char prev = stream.fill('0');  // save current fill character

for(int i=0; i<5; i++)
  stream << setw(2) << MACData[i] << '-';
stream << setw(2) << MACData[5];

stream.fill(prev);  // restore fill character
于 2011-01-03T19:49:10.473 回答
1

First you should convert your code to use C++ streams and manipulators, e.g.

std::cout << std::hex << std::setw(2) << std::setfill('0') << MACData[0] ...

Then you should overload the << operator for a stream on the left side and your class on the right.

于 2011-01-03T19:49:16.697 回答