CUnicodeString Class
CUnicodeString is a replacement for CString. If you are one of these poor guys that has to write applications that use for example NetXxx functions and don't want to create a Unicode project, or use ANSI only functions from a unicode project or what ever, you always had to deal with MultiByteToWideChar and WideCharToMultibyte. Maybe you even have to play with native functions witch uses counted unicode strings (UNICODE_STRING) or LSA (LSAUNICODE_STRING). If you were in such situations and hated the fact that the MFC CString doesn't help you with these conversions, than you would love this little class...
CUnicodeString has all the functions that CString provides, so you can use it allmost everywhere where you use CString normaly. This class works internally only with Unicode strings. Every string assignment is converted to and stored as Unicode.
Example:
CUnicodeString cs; WCHAR wc = L"My Wide String"; cs = wc; CHAR ch = " needs a few ANSI chars to append"; cs += wc; cs += " "; LPSTR pstrMyText = cs; LPWSTR pstrMyUnicodeText = cs; cs.Empty(); cs = "MyUserName"; NetUserGetInfo(cs,....); ...This class was written for a upcomming C++ library called SecLIB witch covers the whole NT-Security. This is one of the reasons why it internally works in Unicode. BTW: Using System API's in Unicode is allways faster in NT, because NT internally works also with Unicode only... :)
I hope you like it! :)
Alexander Keck
MCSE, MCDBA
(MCSD in progress....)
Comments
Arabic text processing
Posted by salton5 on 05/11/2006 04:49pmdear I will appreciate your help very much if you have a class or any comment on processing arabic text. ( for my project) regards Salton
ReplyCUnicodeString::operator += (LPCSTR pstrString)
Posted by allos on 02/24/2006 05:32amHow to solve this
Posted by Kumar_poruki on 03/24/2005 10:47pmif a sequence of input bytes doesnot form a valid character then the function must skip the invalid character or replace with default multibyte character of similar type to continue the conversion from GB to UTF8 ?
ReplyUCS 2
Posted by Legacy on 02/04/2003 12:00amOriginally posted by: Wael
Hello ,
how to convert to UCS 2 and FROM UCS 2
wael
ReplyIs this 32-bit UNICODE or 16-bit UNICODE ?
Posted by Legacy on 01/08/2003 12:00amOriginally posted by: John
UNICODE is a character set that can be expressed in
many different codesets. Which one do you support ?
I'm guessing one of the following, and the intel
byte-ordering version of it:
UCS-2 ? (From the old Unicode 3.0)
UTF-16 ? (A variable width coding)
Problem with WideCharToMultiByte() function
Posted by Legacy on 09/06/2002 12:00amOriginally posted by: Virginie
small bug
Posted by Legacy on 01/01/2002 12:00amOriginally posted by: Tim Monks
CString already works w/ Unicode...
Posted by Legacy on 08/07/2001 12:00amOriginally posted by: sean
CString already works w/ Unicode strings dOOfey.
Reply
MS Provides equivalent in CHString
Posted by Legacy on 02/15/2001 12:00amOriginally posted by: Shane Triem
Microsoft provides the CHString as part of the Platform SDK: Windows Management Instrumentation.
In MSDN, refer to "CHString Class Reference"
I'm sure the class provided Alexander Keck is fine, but I try to stick with the Platform SDK whenever possible. I once posted to CodeGuru how to extract CString from MFC to be used in ATL projects without the MFC DLLs. Now that CHString is available, this is no longer necessary.
Note: the CHString class is in the library Framedyn.lib. This DLL has a footprint of 164 KB.
*************************************
The CHString class is modeled on the Microsoft MFC CString class; the primary difference between CString and CHString is that CHString lacks some of the Unicode support of CString.
Internally, CHString instances (strings) store information as wide character strings; that is, 2 bytes per character. The default value of a CHString string is the starting address of its first character. CHString strings end with a 2-byte null.
Misc Errors with += operator
Posted by Legacy on 11/16/2000 12:00amOriginally posted by: Phillip Trelford
Liked the class, handy for manipulating those passed COM BSTRs, but found some errors when using the += operator.
e.g. Appending to uninitialised string, and the appending of a LPCTSTR.
You might find my changes useful.
void CUnicodeString::operator += (const CUnicodeString& usString)
{
size_t nLen=wcslen(usString);
if( nLen==0 ) return;
m_Length += nLen;
LPWSTR pstrNewString = (LPWSTR)malloc((m_Length + 1) * sizeof(WCHAR));
if( m_Buffer )
{
wcscpy(pstrNewString, m_Buffer);
wcscat(pstrNewString, usString);
free(m_Buffer);
}
else
{
wcscpy(pstrNewString, usString);
}
m_Buffer = pstrNewString;
m_MaximumLength = m_Length;
}
void CUnicodeString::operator += (LPCWSTR pstrString)
{
ASSERT(IsValidString(pstrString));
if( pstrString==NULL ) return;
size_t nLen=wcslen(pstrString);
if( nLen==0 ) return;
m_Length += nLen;
LPWSTR pstrNewString = (LPWSTR)malloc((m_Length + 1) * sizeof(WCHAR));
if( m_Buffer )
{
wcscpy(pstrNewString, m_Buffer);
wcscat(pstrNewString, pstrString);
free(m_Buffer);
}
else
{
wcscpy(pstrNewString, pstrString);
}
m_Buffer = pstrNewString;
m_MaximumLength = m_Length;
}
void CUnicodeString::operator += (LPCSTR pstrString)
{
ASSERT(IsValidString(pstrString));
if( pstrString==NULL ) return;
size_t nLen=strlen(pstrString);
if( nLen==0 ) return;
USHORT usNewLength = m_Length + nLen;
LPWSTR pstrNewString = (LPWSTR)malloc((usNewLength + 1) * sizeof(WCHAR));
if( m_Buffer )
{
wcscpy(pstrNewString, m_Buffer);
}
MultiByteToWideChar(CP_ACP, 0, pstrString, nLen, &pstrNewString;[m_Length], nLen);
pstrNewString[usNewLength]=L'\0';
if( m_Buffer )
{
free(m_Buffer);
}
m_Buffer = pstrNewString;
m_Length = usNewLength;
m_MaximumLength = m_Length;
}
void CUnicodeString::operator += (WCHAR wch)
{
m_Length++;
LPWSTR pstrNewString = (LPWSTR)malloc((m_Length + 1) * sizeof(WCHAR));
if( m_Buffer )
{
wcscpy(pstrNewString, m_Buffer);
}
pstrNewString[m_Length - 1] = wch;
pstrNewString[m_Length] = L'\0';
if( m_Buffer )
{
free(m_Buffer);
}
m_Buffer = pstrNewString;
m_MaximumLength = m_Length;
}
void CUnicodeString::operator += (CHAR ch)
{
USHORT usNewLength = m_Length + 1;
LPWSTR pstrNewString = (LPWSTR)malloc((usNewLength + 1) * sizeof(WCHAR));
if( m_Buffer )
{
wcscpy(pstrNewString, m_Buffer);
}
MultiByteToWideChar(CP_ACP, 0, &ch;, 1, &pstrNewString;[m_Length], usNewLength - m_Length);
pstrNewString[usNewLength] = L'\0';
if( m_Buffer )
{
free(m_Buffer);
}
m_Buffer = pstrNewString;
m_Length = usNewLength;
m_MaximumLength = m_Length;
}
Loading, Please Wait ...