|
发表于 2006-4-4 13:31:23
|
显示全部楼层
eGust用delphi写的。我看不懂。好像是用指针操作的。
- function A2U(us:PChar):PChar;
- var
- ws : PWCHAR;
- cs : PChar;
- nLen : integer;
- begin
- nLen := MultiByteToWideChar(CP_ACP, 0, us, -1, nil, 0);
- GetMem(ws, Sizeof(WCHAR) * (nLen + 2));
- MultiByteToWideChar(CP_ACP, 0, us, -1, ws, nLen);
- nLen := WideCharToMultiByte(CP_UTF8, 0, ws, -1, nil, 0, nil, nil);
- if bolHaveHead then
- begin
- GetMem(cs, Sizeof(char) * (nLen + 4));
- Result := cs;
- cs^ := #$ef;
- Inc(cs);
- cs^ := #$bb;
- Inc(cs);
- cs^ := #$bf;
- Inc(cs);
- end else
- begin
- GetMem(cs, Sizeof(char) * (nLen + 1));
- Result := cs;
- end;
- WideCharToMultiByte(CP_UTF8, 0, ws, -1, cs, nLen, nil, nil);
- FreeMem(ws);
- end;
- function U2A(us:PChar):PChar;
- var ws:PWCHAR;cs:PChar;nLen:integer;
- begin
- if us^=#$ef then
- begin
- Inc(us);
- if us^=#$bb then
- begin
- Inc(us);
- bolHaveHead := us^=#$bf;
- if bolHaveHead then
- Inc(us)
- else
- Dec(us, 2);
- end
- else
- Dec(us);
- end;
- nLen := MultiByteToWideChar(CP_UTF8, 0, us, -1, nil, 0);
- GetMem(ws, sizeof(WCHAR) * (nLen + 2));
- MultiByteToWideChar(CP_UTF8, 0, us, -1, ws, nLen);
- nLen := WideCharToMultiByte(CP_ACP, 0, ws, -1, nil, 0, nil, nil);
- GetMem(cs, sizeof(char) * (nLen + 3));
- Result := cs;
- WideCharToMultiByte(CP_ACP, 0, ws, -1, cs, nLen, nil, nil);
- end;
复制代码 |
|