Delphi如何将hex解码后的数组转为UTF-16BE编码
function HexToStr(str: string): string;
function HexToInt(hex: string): integer;
var
i: integer;
function Ncf(num, f: integer): integer;
var
i: integer;
begin
Result := 1;
if f = 0 then exit;
for i := 1 to f do
result := result * num;
end;
function HexCharToInt(HexToken: char): integer;
begin
if HexToken > #97 then
HexToken := Chr(Ord(HexToken) - 32);
Result := 0;
if (HexToken > #47) and (HexToken < #58) then { chars 0…9 }
Result := Ord(HexToken) - 48
else if (HexToken > #64) and (HexToken < #71) then { chars A…F }
Result := Ord(HexToken) - 65 + 10;
end;
begin
result := 0;
hex := ansiuppercase(trim(hex));
if hex = ‘’ then
exit;
for i := 1 to length(hex) do
result := result + HexCharToInt(hex[i]) * ncf(16, length(hex) - i);
end;
var
s, t: string;
i, j: integer;
p: pchar;
begin
s := ‘’;
i := 1;
while i < length(str) do begin
t := str[i] + str[i + 1];
s := s + chr(hextoint(t));
i := i + 2;
end;
result := s;
end;
上面代码只做了ascii的编码 需要做判断再做UTF16BE的编码,求解