- Код: Выделить всё
' APIs
Private Declare Function LocalAlloc Lib "kernel32.dll" (ByVal wFlags As Long, ByVal wBytes As Long) As Long
Private Declare Function LocalFree Lib "kernel32.dll" (ByVal hMem As Long) As Long
Private Declare Function MultiByteToWideChar Lib "kernel32.dll" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As String, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
Private Declare Function WideCharToMultiByte Lib "kernel32.dll" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long
Private Declare Function GetACP Lib "kernel32" () As Long
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)
' Constants
Private Const LMEM_ZEROINIT As Long = &H40
Public Const WC_COMPOSITECHECK = &H200
Public Const WC_DEFAULTCHAR = &H40
Public Const WC_DISCARDNS = &H10
Public Const WC_SEPCHARS = &H20
Public Const MB_PRECOMPOSED = &H1
Public Const MB_COMPOSITE = &H2
Public Const MB_USEGLYPHCHARS = &H4
Public Const MB_ERR_INVALID_CHARS = &H8
Enum CodePage
CP_ACP = 0
CP_OEMCP = 1
CP_MACCP = 2
CP_THREAD_ACP = 3
CP_SYMBOL = 42
CP_UTF7 = 65000
CP_UTF8 = 65001
CP_Korean = 51949
CP_Windows_1251 = 1251
End Enum
Public Function ConvertString(ByVal inString As String, ByVal nFromCP As CodePage, ByVal nToCP As CodePage) As String
Dim hMemLock1 As Long, hMemLock2 As Long
Dim iStrSize As Long
Dim iStrSize2 As Long
Dim lMaxSize As Byte
lMaxSize = 255
hMemLock1 = LocalAlloc(LMEM_ZEROINIT, lMaxSize)
hMemLock2 = LocalAlloc(LMEM_ZEROINIT, lMaxSize)
iStrSize = MultiByteToWideChar(0&, 0&, inString, &HFFFF, hMemLock1, lMaxSize)
iStrSize = WideCharToMultiByte(65001, 0&, hMemLock1, iStrSize, hMemLock2, lMaxSize, 0&, 0&) ' CP_UTF8
If Len(iStrSize) Then
ConvertString = String$(iStrSize, 0&)
Call CopyMemory(ByVal ConvertString, ByVal hMemLock2, iStrSize)
End If
Call LocalFree(hMemLock1)
Call LocalFree(hMemLock2)
End Function
работающая функция на делфях
- Код: Выделить всё
function KoreanToUnicode(Str: AnsiString): WideString;
var
Size: Integer;
begin
// Attempt to convert from Korean
Size := MultiByteToWideChar(51949, MB_PRECOMPOSED, PChar(Str), -1, nil, 0);
if Size > 0 then
begin
// Don't include NULL character.
Size := Size - 1;
SetLength(Result, Size);
MultiByteToWideChar(51949, MB_PRECOMPOSED, PChar(Str), -1, PWideChar(Result), Size);
end else
begin
// Failed (maybe Korean support is not installed). Convert from ANSI.
Size := MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, PChar(Str), -1, nil, 0);
// Don't include NULL character.
Size := Size - 1;
SetLength(Result, Size);
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, PChar(Str), -1, PWideChar(Result), Size);
end;
end;
для теста могу подкинуть строку °Ўµе- в блокноте именно так и выглядит
Если эту строку открыть в ворде (открывается с корейской кодировкой), а потом сохранить в файле как юникод, то получится следующая строка в хексе 00 AC DС B4
Кручу уже целый день и все безрезультатно.