C
Christopher
I am trying to debug some code that supposedly stores text as bytes
and then back again.
The first step is to examine the contents.
Somewhere data was inserted that contained at least one character with
the signed bit set.
So, the supposed "text" isn't even valid text at all.
I had orginally written this code, which threw if an invalid text
cahr (or non-ascii) was found:
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.
//
// It was assumed only ascii characters would be used, but that
might not be the case
std::wstringstream output;
size_t numBytes = getSize();
for( const_iterator itBuffer = begin(); itBuffer != end(); itBuffer
++ )
{
for( size_t byteIndex = 0; byteIndex < numBytes; ++byteIndex )
{
char & data = itBuffer->buffer->value_.get()[byteIndex];
if( data < 0 )
{
// Error - Invalid byte value
std::wstringstream msg;
msg << L"Attempted to convert byte values to wide
character hex text values and came across a negative signed
character.";
LOG4CXX_ERROR(logger, msg.str());
throw InternalErrorException(__WFILE__, __LINE__) <<
msg.str();
}
output << std::hex << std::setw(2) << std::setfill(L'0')
<< data;
output << "' '";
}
}
return output.str();
}
In debugging, my exception happened.
Can I examine the values just by taking my if statement out? How do I
recognize the signed values in hex?
If this buffer supposedly held XML in ASCII, how would you go about
looking at the contents to see where it became invalid?
and then back again.
The first step is to examine the contents.
Somewhere data was inserted that contained at least one character with
the signed bit set.
So, the supposed "text" isn't even valid text at all.
I had orginally written this code, which threw if an invalid text
cahr (or non-ascii) was found:
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.
//
// It was assumed only ascii characters would be used, but that
might not be the case
std::wstringstream output;
size_t numBytes = getSize();
for( const_iterator itBuffer = begin(); itBuffer != end(); itBuffer
++ )
{
for( size_t byteIndex = 0; byteIndex < numBytes; ++byteIndex )
{
char & data = itBuffer->buffer->value_.get()[byteIndex];
if( data < 0 )
{
// Error - Invalid byte value
std::wstringstream msg;
msg << L"Attempted to convert byte values to wide
character hex text values and came across a negative signed
character.";
LOG4CXX_ERROR(logger, msg.str());
throw InternalErrorException(__WFILE__, __LINE__) <<
msg.str();
}
output << std::hex << std::setw(2) << std::setfill(L'0')
<< data;
output << "' '";
}
}
return output.str();
}
In debugging, my exception happened.
Can I examine the values just by taking my if statement out? How do I
recognize the signed values in hex?
If this buffer supposedly held XML in ASCII, how would you go about
looking at the contents to see where it became invalid?