On Aug 24, 6:25 am, Joseph M. Newcomer <newco...@flounder.com> wrote:
This code assumes the CByteArray does not have a NUL character
CString Buffer((LPCSTR)Array.GetBuffer());
while this relies on a "feature" that many disapprove of, it works very well. The
"feature" is that there is a CString constructor that will accept an 8-bit character
string and create an initialized CString, even if the CString is actually a Unicode
string.
It does not need a concatenation, which can get you into the "exponential growth cost"
problem if the string is very long.
Another approach would be
CByteArray Array;
...
CString Buffer = A2T((LPCSTR)Array.GetBuffer());
On Fri, 24 Aug 2007 01:06:33 +0200, "Giovanni Dicanio" <giovanni.dica...@invalid.it>
wrote:
"AliR (VC++ MVP)" <A...@online.nospam> ha scritto nel messaggio
news:SFhzi.4581$LL7.4217@nlpi069.nbdc.sbc.com...
What kind of conversion are we talking about, what is the byte array
representing? Does the byte array have 0 anywhere in it?
Here is one way
CString Buffer;
for (int i = 0; i < Array.Length();i++)
{
Buffer += (TCHAR)Array.GetAt(i);
}
Hello Alir,
I don't want to be picky, but I don't like very much the above TCHAR cast...
I think that the above code could be OK *without the TCHAR cast*, if Array
stores BYTEs, which are the ANSI characters to be put in the CString.
But if the input array stores bytes for Unicode UTF-16 strings, I think that
it would not be correct.
I think that a code like the following could be OK, with two separate
branches for ANSI and Unicode (UTF-16):
<code>
///////////////////////////////////////////////////////////
// An array storing BYTEs
///////////////////////////////////////////////////////////
typedef std::vector< BYTE > ByteArray;
///////////////////////////////////////////////////////////
// Build a ANSI string from characters stored into a
// byte array
///////////////////////////////////////////////////////////
CStringA BuildAnsiString( const ByteArray & chars )
{
if ( chars.empty() )
return "";
const int charsCount = static_cast<int>( chars.size() );
CStringA str( ' ', charsCount );
for ( int i = 0; i < charsCount; i++ )
{
str.SetAt( i, chars[ i ] );
}
return str;
}
****
This seems to be an unnecessarily complicated way to deal with this. Given that it can be
written in a single line of code using the constructor, the entire body could be
return CStringA((LPCSTR)chars.GetBuffer());
and it would also be faster, particularly in debug mode.
joe
****
///////////////////////////////////////////////////////////
// Build a Unicode UTF-16 string from characters stored
// into a byte array
///////////////////////////////////////////////////////////
CStringW BuildUnicodeString( const ByteArray & chars )
{
if ( chars.empty() )
return L"";
// Unicode chars count
const int charsCount = static_cast<int>( chars.size() ) / 2;
CStringW str( ' ', charsCount );
const BYTE * pBytes = &chars[0];
for ( int i = 0; i < charsCount; i++ )
{
// Build the Unicode UTF-16 code-point
WCHAR wch = MAKEWORD( *pBytes, *(pBytes+1) );
// Point to next WCHAR in source array
pBytes += 2;
// Update string character
str.SetAt( i, wch );
}
return str;
}
****
But the body could be
return CStringW((LPCSTR)chars.GetBuffer());
which is a lot simpler to write.
joe
****
//
// TEST
//
void Test()
{
//
// ANSI Test
//
ByteArray ansiChars;
ansiChars.push_back( 0x48 ); // H
ansiChars.push_back( 0x49 ); // I
CStringA strAnsi = BuildAnsiString( ansiChars );
CA2W s( strAnsi );
AfxMessageBox( s );
//
// Unicode Test
//
ByteArray charsUnicode;
charsUnicode.push_back( 0xB1 ); // alpha
charsUnicode.push_back( 0x03 );
charsUnicode.push_back( 0xB4 ); // delta
charsUnicode.push_back( 0x03 );
CStringW strUnicode = BuildUnicodeString( charsUnicode );
AfxMessageBox( strUnicode );
}
</code>
Maybe the OP was not very clear about the content of his byte-array...
Giovanni
Joseph M. Newcomer [MVP]
email: newco...@flounder.com
Web:http://www.flounder.com
MVP Tips:http://www.flounder.com/mvp_tips.htm