Re: stream bytes
Christopher <cpisz@austin.rr.com> wrote in news:1b7250d5-8fff-4209-bfe3-
435d0b2e95f3@u32g2000yqe.googlegroups.com:
Later down the road, someone must have inserted something besides
UTF-8 encoded text. My goal is to identify where that is occuring. My
thought was to examine the contents of the buffer and see what parts
are able to be converted to UTF-8 encoded text, look at it, and see if
UTF-8 is quite well detectable in an unknown bytestream as it has been
designed with this goal in mind.
How do I dump the contents out as a textual representation of the
bytes? Would the code below do the trick?
std::wstring StringBufferList::GetBytesAsText() const
{
// This class should have been storing bytes as unsigned char
rather than char
// to begin with and needs to be changed later.
//
// I am just adding this method quickly for debugging purposes.
//
// Because of the lack of type safety currently in insertion of
any type using a reinterpret_cast
// this class made use of, we must check each byte for validity.
std::wstringstream output;
size_t numBytes = getSize();
for( const_iterator itBuffer = begin(); itBuffer != end(); itBuffer
++ )
{
for( size_t byteIndex = 0; byteIndex < numBytes; ++byteIndex )
{
int & byte = static_cast<int>(itBuffer->buffer-
value_.get()[byteIndex]);
Have you tried to compile this? And why are you so obsessed by references
to scalar items? I guess this is what you want:
int byte = static_cast<unsigned char>
(itBuffer->buffer->value_.get()[byteIndex]);
(assuming the parenthized expression yields a char). You have to first
convert to unsigned char in order to get the correct negative->positive
value translation, and then to int in order to see the hexadecimal value
instead of "literal byte" in the stream.
output << std::hex << std::setw(2) << std::setfill(L'0')
<< byte;
output << "' '";
}
}
return output.str();
}
hth
Paavo