function TForm1.HexStrToStr(const S:string):string;
var
t:Integer;
ts:string;
M,Code:Integer;
begin
t:=1;
Result:='';
while t<=Length(S) do
begin //xlh 2006.10.21
while (t<=Length(S)) and (not (S[t] in ['0'..'9','A'..'F','a'..'f'])) do
inc(t);
if (t+1>Length(S))or(not (S[t+1] in ['0'..'9','A'..'F','a'..'f'])) then
ts:='$'+S[t]
else
ts:='$'+S[t]+S[t+1];
Val(ts,M,Code);
if Code=0 then
Result:=Result+Chr(M);
inc(t,2);
end;
end;
var
t:Integer;
ts:string;
M,Code:Integer;
begin
t:=1;
Result:='';
while t<=Length(S) do
begin //xlh 2006.10.21
while (t<=Length(S)) and (not (S[t] in ['0'..'9','A'..'F','a'..'f'])) do
inc(t);
if (t+1>Length(S))or(not (S[t+1] in ['0'..'9','A'..'F','a'..'f'])) then
ts:='$'+S[t]
else
ts:='$'+S[t]+S[t+1];
Val(ts,M,Code);
if Code=0 then
Result:=Result+Chr(M);
inc(t,2);
end;
end;
public String HexStrToStr(String S)
{
int t = 0;
String ts,Result;
int M, Code;
Result = "";
while (t < S.Length())
{
while ((t < S.Length()) && (!(((S[t] >=0) && (S[t] <=9)) || ((S[t] >='A') && (S[t] <='F')) || ((S[t] >='0') && (S[t] <='f')))))
{
t++;
} if (((t + 1) >= S.Length()) || (!(((S[t] >= 0) && (S[t] <= 9)) || ((S[t] >= 'A') && (S[t] <= 'F')) || ((S[t] >= '0') && (S[t] <= 'f')))))
{
ts = "$" + S[t];
}
else
{
ts = "$" + S[t] + S[t + 1];
}
try
{
M == Convert.ToInt32(ts);
Code = 0;
}
catch
{
Code = 1;
}
if (Code == 0)
{
Result = Result + Convert.ToChar(M);
}
t = t + 2;
}
}呵呵!翻译得好傻!
百度上有现成函数的!
搜搜看看吧!
比你自己写的要好一点儿!
{
string Result = string.Empty;
int t = 0;
string ts = string.Empty;
int M;
while (t < S.Length)
{
while (t < S.Length && !((S[t] >= '0' && S[t] <= '9') || (S[t] >= 'a' && S[t] <= 'f') || (S[t] >= 'A' && S[t] <= 'F')))
t++;
if (t + 1 > S.Length - 1)
{
ts = "0x" + S[t];
}
else
ts = "0x" + S[t] + S[t + 1];
try
{
M = Convert.ToInt32(ts,16); Result = Result + Convert.ToChar(M);
}
catch
{
}
t = t + 2;
}
return Result;
}