编码:
function TfrmMain.str_Gb2UniCode( text: string ): String;
var
i,j,len:Integer;
cur:Integer;
t:String;
ws:WideString;
begin
Result:='';
ws := text;
len := Length(ws);
i := 1;
j := 0;
while i <= len do
begin
cur := ord(ws);
FmtStr(t,'%4.4X',[cur]); //BCD转换
Result := Result+t;
inc(i);
//移位计数达到7位的特别处理
j := (j+1) mod 7;
end;
end;
解码:
function UnicodeToString(var AString: string; AUnicode: PChar; ALenth: integer): integer;
var
TmpBuf: array [1..1024] of char;
TmpChar: char;
TmpLen, i: integer;
begin
try
TmpLen := ALenth div 2;
if ALenth > 0 then
begin
CopyMemory(@TmpBuf, AUnicode, ALenth);
for i := 0 to TmpLen - 1 do
begin
TmpChar := (AUnicode + i * 2)^;
(AUnicode + i * 2)^ := (AUnicode + i * 2 + 1)^;
(AUnicode + i * 2 + 1)^ := TmpChar;
end;
TmpBuf[TmpLen * 2 + 1] := #0;
TmpBuf[TmpLen * 2 + 2] := #0;
AString := WideCharToString(pwidechar(AUnicode));
Result := TmpLen;
end
else
begin
Result := 0;
end;
except
AString := '';
Result := 0;
end;
end;