读书人

如何判断文本是不是UNICODE书本上有

发布时间: 2012-05-28 17:59:54 作者: rapoo

怎么判断文本是不是UNICODE,书本上有错了,你们都看看
BOOL PopFileRead (HWND hwndEdit, PTSTR pstrFileName)
{
BYTE bySwap ;
DWORD dwBytesRead ;
HANDLE hFile ;
int i, iFileLength, iUniTest ;
PBYTE pBuffer, pText, pConv ;

// Open the file.

if (INVALID_HANDLE_VALUE ==
(hFile = CreateFile (pstrFileName, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, 0, NULL)))
return FALSE ;

// Get file size in bytes and allocate memory for read.
// Add an extra two bytes for zero termination.

iFileLength = GetFileSize (hFile, NULL) ;
pBuffer = malloc (iFileLength + 2) ;

// Read file and put terminating zeros at end.

ReadFile (hFile, pBuffer, iFileLength, &dwBytesRead, NULL) ;
CloseHandle (hFile) ;
pBuffer[iFileLength] = '\0' ;
pBuffer[iFileLength + 1] = '\0' ;

// Test to see if the text is Unicode

iUniTest = IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE ;

if (IsTextUnicode (pBuffer, iFileLength, &iUniTest))
{
pText = pBuffer + 2 ;
iFileLength -= 2 ;

if (iUniTest & IS_TEXT_UNICODE_REVERSE_SIGNATURE)
{
for (i = 0 ; i < iFileLength / 2 ; i++)
{
bySwap = ((BYTE *) pText) [2 * i] ;
((BYTE *) pText) [2 * i] = ((BYTE *) pText) [2 * i + 1] ;
((BYTE *) pText) [2 * i + 1] = bySwap ;
}
}

// Allocate memory for possibly converted string

pConv = malloc (iFileLength + 2) ;

// If the edit control is not Unicode, convert Unicode text to
// non-Unicode (ie, in general, wide character).

#ifndef UNICODE
WideCharToMultiByte (CP_ACP, 0, (PWSTR) pText, -1, pConv,
iFileLength + 2, NULL, NULL) ;

// If the edit control is Unicode, just copy the string
#else
lstrcpy ((PTSTR) pConv, (PTSTR) pText) ;
#endif

}
else // the file is not Unicode
{
pText = pBuffer ;

// Allocate memory for possibly converted string.

pConv = malloc (2 * iFileLength + 2) ;

// If the edit control is Unicode, convert ASCII text.

#ifdef UNICODE
MultiByteToWideChar (CP_ACP, 0, pText, -1, (PTSTR) pConv,
iFileLength + 1) ;

// If not, just copy buffer
#else
lstrcpy ((PTSTR) pConv, (PTSTR) pText) ;
#endif
}

SetWindowText (hwndEdit, (PTSTR) pConv) ;
free (pBuffer) ;
free (pConv) ;

return TRUE ;
}
这个的读取文件的代码,就是首先要判断这个文本是不是UNICODE文本,我就有个疑问,就是要是这个文本是UNICODE就执行第一个IF里的语言,否则就执行IF的else 语言,你们看,要是我选择的文本是UNICODE文本,就直接执行if (IsTextUnicode (pBuffer, iFileLength, &iUniTest))
{
pText = pBuffer + 2 ;
iFileLength -= 2 ;

if (iUniTest & IS_TEXT_UNICODE_REVERSE_SIGNATURE)
{
for (i = 0 ; i < iFileLength / 2 ; i++)
{


bySwap = ((BYTE *) pText) [2 * i] ;
((BYTE *) pText) [2 * i] = ((BYTE *) pText) [2 * i + 1] ;
((BYTE *) pText) [2 * i + 1] = bySwap ;
}
}

// Allocate memory for possibly converted string

pConv = malloc (iFileLength + 2) ;

// If the edit control is not Unicode, convert Unicode text to
// non-Unicode (ie, in general, wide character).

这个语句,是把,但是下面这个#ifdef UNICODE
MultiByteToWideChar (CP_ACP, 0, pText, -1, (PTSTR) pConv,
iFileLength + 1) ;

// If not, just copy buffer
#else
lstrcpy ((PTSTR) pConv, (PTSTR) pText) ;
#endif
不是多余的吗,我既然已经确定了这个文本是UNICODE文本直接执行这个就是的,在将字符串复制进来就不OK了,为什么还要----要是定义了UNICODE标识符,就需要将UNICODE字符转换为多字节的,这个不是多余的,你们有什么见解,请发言,谢谢

[解决办法]
看前两个字节是不是0xff,0xfe
[解决办法]

探讨

看前两个字节是不是0xff,0xfe

读书人网 >C++

热点推荐