lundi 29 juin 2015

Print to stdin, in hex or decimal, a std::string as a sequence of bytes

The API I am working with returns a sequence of bytes as a std::string.

How do I print this to stdin, formatted as a sequence, either in hex or decimal.

This is the code I am using:

        int8_t i8Array[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
        std::string i8InList(reinterpret_cast<const char*>(i8Array), 16);
        std::string i8ReturnList;

        client.GetBytes(i8ReturnList, i8InList);

        cout << "GetBytes returned ";
        std::copy(i8ReturnList.begin(), i8ReturnList.end(),   std::ostream_iterator<int8_t>(std::cout << " " ));

I am expecting to receive the same input, but in reverse order. What this code prints however is:

GetBytes returned   ☺☻♥♦♣
♫☼

NOTE: The sequence of bytes is an arbitrary sequence of bytes, it is not a representation of written language.

Aucun commentaire:

Enregistrer un commentaire