Als je dit doet zul je zien dat de originele tekst EveXml1.txt en de tekst
EveXml1-UTF8.txt gelijk zijn op 1 karakter na. Dat komt doordat voor dat
karakter (A met een horizontaal streepje erboven) in de Ansi codepage geen
karakter bestaat. Dit wordt dan vertaald tot een gewone A.
FUNCTION Start()
LOCAL cStr AS STRING
cStr := MemoRead("C:\EveXml1.txt")
//
// Eerste 3 karakters geven dan aan dat het UTF8 is.
// Als de eerste 2 karakters 0xFF 0xFE zijn is het Unicode
//
IF Left(cStr,3) == CHR(0xEF)+CHR(0xBB)+CHR(0xBF)
cStr := Utf82Ansi(SubStr(cStr,4))
ENDIF
MemoWrit("C:\EveXml1-Ansi.txt", cStr)
cStr := Ansi2UTF8(cStr)
MemoWrit("C:\EveXml1-UTF8.txt", CHR(0xEF)+CHR(0xBB)+CHR(0xBF)+cStr)
WAIT
FUNCTION Utf82Ansi(cString AS STRING) AS STRING
LOCAL pUStr AS PTR
LOCAL pBuffer AS PTR
LOCAL nLen,nULen AS DWORD
// Convert VO string from dynamic memory to fixed memory
nLen := SLen(cString)
pBuffer := MemAlloc(nLen)
MemCopyString(pBuffer,cString,nLen)
// Determine length of Unicode string
// And allocate enough space to hold it
nULen := MultiByteToWideChar(CP_UTF8,0,pBuffer,LONG(nLen),NULL_PTR, 0)
pUStr := SysAllocStringLen(NULL_PTR,nULen)
// Convert Fixed memory UTF8 string to Fixed memory Unicode string
MultiByteToWideChar(CP_UTF8,0,pBuffer,LONG(nLen),pUStr,LONG(nULen))
// Release Fixed memory UTF buffer
MemFree(pBuffer)
// Now determine size needed for ANSI string
nLen := WideCharToMultiByte(CP_ACP,0,pUStr,nULen,NULL_PTR,0, NULL,NULL)
// Allocate Fixed memory buffer to hold the ansi string
pBuffer := MemAlloc(nLen+1)
// Convert Unicode to Ansi
nLen := WideCharToMultiByte(CP_ACP,0,pUStr,nULen,pBuffer,LONG(nLen ),NULL,NULL)
// Convert fixed memory buffer to dynamic memory string
cString := Mem2String(pBuffer,nLen)
// Release fixed memory buffer
MemFree(pBuffer)
// Release the Unicode String
SysFreeString(pUStr)
RETURN cString
FUNCTION Ansi2UTF8(cString AS STRING) AS STRING
LOCAL pUStr AS PTR
LOCAL pBuffer AS PTR
LOCAL nLen,nULen AS DWORD
// Convert VO string from dynamic memory to fixed memory
nLen := SLen(cString)
pBuffer := MemAlloc(nLen)
MemCopyString(pBuffer,cString,nLen)
// Determine length of Unicode string
// And allocate enough space to hold it
nULen := MultiByteToWideChar(CP_ACP,0,pBuffer,LONG(nLen),NULL_PTR, 0)
pUStr := SysAllocStringLen(NULL_PTR,nULen)
// Convert Fixed memory Ansi string to Fixed memory Unicode string
MultiByteToWideChar(CP_ACP,0,pBuffer,LONG(nLen),pUStr,LONG(nULen))
// Release Fixed memory UTF buffer
MemFree(pBuffer)
// Now determine size needed for ANSI string
nLen := WideCharToMultiByte(CP_UTF8,0,pUStr,nULen,NULL_PTR,0, NULL,NULL)
// Allocate Fixed memory buffer to hold the UTF8 string
pBuffer := MemAlloc(nLen+1)
// Convert Unicode to Ansi
nLen := WideCharToMultiByte(CP_UTF8,0,pUStr,nULen,pBuffer,LONG(nLen ),NULL,NULL)
// Convert fixed memory buffer to dynamic memory string
cString := Mem2String(pBuffer,nLen)
// Release fixed memory buffer
MemFree(pBuffer)
// Release the Unicode String
SysFreeString(pUStr)
RETURN cString
--
Robert van der Hulst