delphiucs2gb.txt

来自「Delphi函数源代码,GB和UCS2互转换」· 文本 代码 · 共 32 行

TXT
32
字号
function Ucs2ToGB2312(const InValue: string): string;
var
  I, asc: Integer;
begin
  Result := '';
  for I := 1 to Length(InValue) div 2 do
  begin
    asc :=Ord(InValue[I *2 -1]);
    if asc<>$20 then
      Result := Result + WideChar(StrToInt('$' + IntToHex(Ord(asc), 2) + IntToHex(Ord(InValue[I *2]), 2)))
    else
      Result := Result + Chr(StrToInt('$' +IntToHex(Ord(InValue[I *2]),2)));
  end;
end;

function GB2312ToUcs2(const InValue: string): string;
var
  i : Integer;
  ucs4 : UCS4String;
  ucs2 : String;
begin
  Result := '';
  ucs4 := WideStringToUCS4String(InValue);
  for i:=Low(ucs4) to High(ucs4)-1 do
  begin
    ucs2 := IntToHex(ucs4[i],4);
    if ucs4[i]<$20 then //$7F
      Result :=Result + ' ' + Chr(StrToInt('$'+ucs2))
    else
      Result := Result + Chr(StrToInt('$'+Copy(ucs2,1,2))) + Chr(StrToInt('$'+Copy(ucs2,3,2)));
  end;
end;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?