public static final byte[] hexToByteArray(String str, boolean rev) {
StringBuffer acc = new StringBuffer(str.length() + 1);
int cx, rp, ff, val;
char[] s = new char[str.length()];
str.toLowerCase().getChars(0, str.length(), s, 0);
for (cx = str.length() - 1, ff = 0; cx >= 0; cx--) {
if (hexDigitChars.indexOf(s[cx]) >= 0) {
acc.append(s[cx]);
ff++;
} else {
if ((ff % 2) > 0) acc.append('0');
ff = 0;
}
}
if ((ff % 2) > 0) acc.append('0'); byte[] ret = new byte[acc.length() / 2];
for (cx = 0, rp = ret.length - 1; cx < acc.length(); cx++, rp--) {
val = hexDigitChars.indexOf(acc.charAt(cx));
cx++;
val += 16 * hexDigitChars.indexOf(acc.charAt(cx));
ret[rp] = (byte) val;
}
if (rev) {
byte tmp;
int fx, bx;
for (fx = 0, bx = ret.length - 1; fx < (ret.length / 2); fx++, bx--) {
tmp = ret[bx];
ret[bx] = ret[fx];
ret[fx] = tmp;
}
}
return ret;
}这是一个16进制字符串转换为BYTE数组的函数,我要把它转换为DELPHI的函数?
var
tempStr : String ;
i : Integer;
iValue : byte;
begin
tempStr := str;
for i:= 0 to Length(str) div 2 -1 do
begin
iValue := StrToInt('$'+StrToInttempStr[2*i+1]+tempStr[2*i+2]);
ret[i] := iValue
end;
end;
上面的代码就可以解决
不要看Java的代码 在Delphi中是很容易解决的
var
tempStr : String ;
i : Integer;
iValue : byte;
begin
tempStr := str;
for i:= 0 to Length(str) div 2 -1 do
begin
iValue := StrToInt('$'+StrToInttempStr[2*i+1]+tempStr[2*i+2]);
ret[i] := iValue
end;
end;
var
tempStr : String ;
i : Integer;
iValue : byte;
begin
tempStr := str;
for i:= 0 to Length(str) div 2 -1 do
begin
iValue := StrToInt('$'+StrToInttempStr[2*i+1]+tempStr[2*i+2]);
ret[i] := iValue
end;
end;
//函数
type
TBytes = array of Byte;function TForm1.BytesOfTest(Val: Integer): TBytes;
var
Len: Integer;
begin
Len := sizeof(Val);
SetLength(Result, Len);
Move(val,result[0],Len );
end;
//调用
procedure TForm1.Button1Click(Sender: TObject);
var
A:TBytes;
x:Integer;
begin
A:=BytesOfTest($10);
for x:=0 to length(A)-1 do
begin
showmessage(Inttostr(A[x]))
end;end;